module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=1.0.0"
  environment        = var.Environment
  application_family = var.application_family
  service_name       = var.service_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/ows-promo-player/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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 "promo_player_rds" {
  source = "git@github.com:theorchard/terraform-rds.git//?ref=6.3.0"

  providers = {
    aws.dns = aws.networking
  }

  environment        = var.Environment
  service_name       = var.service_name
  application_family = var.application_family

  rds_master_username = "promo_player_admin"
  rds_master_password = "s0mePass!changme"

  rds_cluster_instance_class                = "db.t4g.medium"
  rds_cluster_instance_count                = "1"
  rds_db_cluster_parameter_group_name       = "aurora-mysql80sync"
  rds_cluster_instance_parameter_group_name = "default.aurora-mysql8.0"
  rds_engine                                = "aurora-mysql"
  rds_engine_version                        = "8.0.mysql_aurora.3.05.2"
  rds_db_subnet_group_name                  = "prod-orchard-private-subnet-group"
  rds_availability_zones                    = ["us-east-1a", "us-east-1c"]
  rds_performance_insights_enabled          = false
  enabled_cloudwatch_logs_exports           = ["audit", "error", "general"]

  custom_kms_key_enabled = false

  vpc_id          = module.vpc_info.vpc_id
  route53_zone_id = data.aws_route53_zone.route53_zone.zone_id
}

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

  environment_name = var.Environment
  service_name     = var.service_name
}

module "secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.3.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_promo_player_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
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 2
  task_cpu                                 = 512
  task_memory                              = 1024
  maximum_capacity                         = 4
  minimum_capacity                         = 2
  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]
  http_listener_enabled                    = false
  blocking_waf_enabled                     = false

  # overriding here to add in the private subnets
  qa_https_listener_allow_cidr_blocks = [
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
    "10.40.0.0/22",
    "10.30.0.0/22",
    "10.10.40.0/22",
  ]

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/Dynamo-qa-ows-promo-player-owsrequest",
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.Environment
    },
    {
      LOGGER_DSN = "https://${var.Environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      MYSQL_DB_NAME = "promo_player"
    },
    {
      MYSQL_DB_HOST = var.promo_player_db_url
    },
    {
      MYSQL_DB_USER = "ows_promo_player"
    }
  ]
  secrets = [
    {
      SENTRY_DSN = "${var.Environment}/${var.service_name}/SENTRY_DSN"
    },
    {
      DB_URL = "${var.Environment}/${var.service_name}/DB_URL"
    }
  ]
}

module "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
  service_4xx_monitor_enabled       = false
  service_5xx_monitor_enabled       = false
  notification_endpoints            = ""
  escalation_notification_endpoints = ""
}
