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          = var.team_name
}

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 backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/promo-theorchard-com/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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

  environment = var.environment
}

data "aws_acm_certificate" "qaorchdotcom" {
  domain      = "*.qaorch.com"
  statuses    = ["ISSUED"]
  most_recent = true
}

data "aws_acm_certificate" "qapdesuitedotcom" {
  domain   = "*.qapdesuite.com"
  statuses = ["ISSUED"]
}

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

module "secrets" {
  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 "promo_theorchard_com_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=6.4.3"

  providers = {
    aws.dns = aws.networking
  }

  environment                   = var.environment
  service_name                  = var.service_name
  aws_region                    = var.aws_region
  application_family            = var.application_family
  commit_sha                    = "latest"
  container_port                = "3000"
  task_type                     = "web_service"
  desired_task_count            = 2
  task_cpu                      = 512
  task_memory                   = 1024
  maximum_capacity              = 4
  minimum_capacity              = 2
  vpc_id                        = module.vpc_info.vpc_id
  https_listener_certificate_id = split("/", data.aws_acm_certificate.qaorchdotcom.arn)[1]
  health_check_path             = "/hello/"

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets   = module.vpc_info.default_private_subnet_ids

  https_listener_allow_prefix_list_names = [
    "sme-infosec-vm-network",
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      AWS_REGION = var.aws_region
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      BFF_PROMO_PLAYER_URL = "https://${var.environment}-bff-promo-player.theorchard.io"
    },
    {
      SCRIPT_SRC = "https://${var.environment}-cdn.theorchard.io"
    },
    {
      IMG_SRC = "https://${var.environment}-images.theorchard.io"
    },
    {
      DEFAULT_SRC = "https://${var.environment}-aws-wowza.qaorch.com,https://${var.environment}-aws-wowza.qaawal.com,https://${var.environment}-aws-wowza.qapdesuite.com"
    },
    {
      MEDIA_SRC = "https://${var.environment}-aws-wowza.qaorch.com,https://${var.environment}-aws-wowza.qaawal.com,https://${var.environment}-aws-wowza.qapdesuite.com"
    },
  ]
  secrets = [
    {
      SEGMENT_KEY = "${var.environment}/${var.service_name}/SEGMENT_KEY"
    },
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
  ]
}

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

  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  application_family                = var.application_family
  service_4xx_monitor_enabled       = false
  service_5xx_monitor_enabled       = false
  healthy_tasks_monitor_enabled     = false
  service_cpu_monitor_enabled       = false
  failed_tasks_monitor_enabled      = false
  successful_tasks_monitor_enabled  = false
  notification_endpoints            = ""
  escalation_notification_endpoints = ""
  teams                             = [var.application_family]
}

resource "aws_lb_listener_certificate" "qapdesuitedotcom" {
  listener_arn    = module.promo_theorchard_com_fargate_environment.fargate_load_balancer_https_listener_arn
  certificate_arn = data.aws_acm_certificate.qapdesuitedotcom.arn
}

data "cloudflare_zone" "cloudflare_zone" {
  name = var.domain_name
}

resource "cloudflare_record" "dns_record" {
  name    = "promo"
  proxied = false
  ttl     = 1
  type    = "CNAME"
  value   = module.promo_theorchard_com_fargate_environment.fargate_load_balancer_dns_name
  zone_id = data.cloudflare_zone.cloudflare_zone.id
}
