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 {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/ows-delivery-history/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

module "secrets_ows_delivery_history" {
  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
}

data "aws_iam_policy" "s3_delivery_xml_bucket_default_ro_policy" {
  name = "S3-${var.delivery_xml_bucket_name}-read-only"
}

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

  environment_name = var.environment
  service_name     = var.service_name
  # Match the existing policy's description (its own name) so the imported
  # policy is adopted in place rather than destroyed and recreated.
  policy_description = "Dynamo-${var.environment}-${var.service_name}-owsrequest"
}

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

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

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

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

  providers = {
    aws.dns = aws.networking
  }

  environment        = var.environment
  service_name       = var.service_name
  application_family = var.application_family
  aws_region         = var.aws_region
  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

  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

  iam_managed_policy_attachments = [
    module.ows_delivery_history_owsrequest.policy_arn_output,
    data.aws_iam_policy.s3_delivery_xml_bucket_default_ro_policy.arn,
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      LOGGER_DSN = "https://prod-fluentd-applications.theorchard.io:8888/application"
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      SENTRY_DSN = module.sentry_ows_delivery_history.sentry_key_dsn_public_output
    },
    {
      DB_USER = var.db_user
    },
    {
      DB_NAME = var.db_name
    },
    {
      DB_HOST = module.ows_delivery_history_rds.rds_cluster_endpoint
    },
    {
      AR_DB_USER = var.ar_db_user
    },
    {
      AR_DB_NAME = var.ar_db_name
    },
    {
      AR_DB_HOST = var.ar_db_host
    },
    {
      DD_DB_USER = var.dd_db_user
    },
    {
      DD_DB_HOST = var.dd_db_host
    },
    {
      DD_DB_DATABASE = var.dd_db_database
    },
    {
      DELIVERY_XML_S3_BUCKET_NAME = var.delivery_xml_bucket_name
    },
  ]
}

module "ows_delivery_history_rds" {
  source = "git@github.com:theorchard/terraform-rds.git//?ref=6.6.0"

  providers = {
    aws.dns = aws.networking
  }

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

  rds_master_username = "ows_delivery_history_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.10.3"
  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 "fargate_service_dashboard" {
  source             = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.4"
  environment        = var.environment
  environment_type   = "fargate"
  service_name       = var.service_name
  application_family = var.application_family

  dependent_applications = var.dependent_applications

  service_cpu_time_window = var.cpu_time_window

  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints

  healthy_tasks_notification_overrides = {
    endpoints            = var.healthy_tasks_notification_endpoints
    escalation_endpoints = var.healthy_tasks_notification_endpoints
  }
}

module "sentry_ows_delivery_history" {
  source             = "git@github.com:theorchard/terraform-sentry.git//?ref=4.1.2"
  service_name       = var.service_name
  environment        = var.environment
  application_family = var.application_family
  platform           = "python"
  teams              = [var.environment]
}
