module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
  team_name          = "content-review-management"
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.aws_region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/ows-product-review/terraform.tfstate"
    region  = "us-east-1"
    encrypt = true
  }
}

data "aws_caller_identity" "current" {}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git?ref=3.0.2"

  environment = var.environment
}

data "aws_acm_certificate" "theorchard_io" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

data "aws_route53_zone" "route53_zone" {
  name = "theorchard.io"
}

module "ows_product_review_sentry_project" {
  source = "git@github.com:theorchard/terraform-sentry.git?ref=4.1.2"

  environment        = var.environment
  platform           = "python"
  service_name       = var.service_name
  application_family = var.application_family
}

module "ows_product_review_owsrequest" {
  source = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.0.1"

  environment_name = var.environment
  service_name     = var.service_name
}

module "multiple_secrets_with_same_settings" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.secrets_manager_secret_names)

  environment                    = var.environment
  service_name                   = var.service_name
  secret_name                    = each.value
  application_family             = var.application_family
  secret_recovery_window_in_days = 7
}

module "ows_product_review_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.5.1"

  providers = {
    aws.dns = aws.networking
  }

  environment                              = var.environment
  service_name                             = var.service_name
  aws_region                               = var.aws_region
  application_family                       = var.application_family
  additional_tags                          = var.additional_tags
  desired_task_count                       = 2
  minimum_capacity                         = 2
  maximum_capacity                         = 4
  task_cpu                                 = 512
  task_memory                              = 1024
  route53_zone_id                          = data.aws_route53_zone.route53_zone.zone_id
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  vpc_id                                   = module.vpc_info.vpc_id
  https_listener_certificate_id            = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  fargate_service_subnets                  = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets                    = module.vpc_info.default_private_subnet_ids

  blocking_waf_enabled = true

  iam_managed_policy_attachments = [
    module.ows_product_review_owsrequest.policy_arn_output,
    aws_iam_policy.start_sfn_content_review_complete_ows_policy.arn
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      MYSQL_DB_USER = "ows-product-review"
    },
    {
      MYSQL_DB_HOST = var.content_review_db_url
    },
    {
      MYSQL_DB_NAME = "content_review"
    },
    {
      SENTRY_DSN = module.ows_product_review_sentry_project.sentry_key_dsn_public_output
    },
    {
      KAFKA_BOOTSTRAP_SERVERS = var.kafka_bootstrap_servers
    },
    {
      VAPI_BASE_URL = var.vapi_base_url
    },
    {
      VAPI_CLIENT_ID = var.vapi_client_id
    },
    {
      VAPI_REDIRECT_URI = var.vapi_redirect_uri
    },
    {
      VAPI_USER_ID = var.vapi_user_id
    },
    {
      VAPI_USER_TYPE = var.vapi_user_type
    },
    # qa-only settings below
    {
      AR_DB_NAME = "art_relations"
    },
    {
      AR_DB_HOST = "qa.db.qaorch.com"
    },
    {
      AR_DB_USER = "ows-product-rvw"
    },
    {
      SPLITIO_USE_PREFORKED_INITIALIZATION = var.splitio_preforked
    },
    {
      SFN_COMPLETE_REVIEW_ARN = data.aws_sfn_state_machine.sfn_content_complete_review.arn
    },
    {
      SFN_COMPLETE_REVIEW_V2_ARN = data.aws_sfn_state_machine.sfn_content_complete_review_v2.arn
    }
  ]
}

module "ows_product_review_fargate_service_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.10.7"

  environment        = var.environment
  environment_type   = "fargate"
  service_name       = var.service_name
  application_family = var.application_family

  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
}
