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

provider "aws" {
  region = local.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

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

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/ows-delivery-metadata/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.1.0"
  environment = local.environment
}

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

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

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

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

  environment                       = local.environment
  environment_type                  = "fargate"
  service_name                      = local.service_name
  application_family                = local.application_family
  notification_endpoints            = local.notification_endpoints
  escalation_notification_endpoints = local.escalation_notification_endpoints

  service_cpu_time_window              = local.cpu_time_window
  service_cpu_critical_number          = local.cpu_critical_number
  service_cpu_critical_recovery_number = local.cpu_critical_recovery_number
  service_cpu_warning_number           = local.cpu_warning_number
  service_cpu_warning_recovery_number  = local.cpu_warning_recovery_number

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

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

  providers = {
    aws.dns = aws.networking
  }

  environment                   = local.environment
  service_name                  = local.service_name
  aws_region                    = local.aws_region
  application_family            = local.application_family
  desired_task_count            = local.fargate_desired_task_count
  minimum_capacity              = local.fargate_minimum_capacity
  maximum_capacity              = local.fargate_maximum_capacity
  task_cpu                      = local.fargate_task_cpu
  task_memory                   = local.fargate_task_memory
  commit_sha                    = "latest"
  container_port                = 8080
  task_type                     = "web_service"
  vpc_id                        = module.vpc_info.vpc_id
  https_listener_certificate_id = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  http_listener_enabled         = false
  fargate_service_subnets       = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets         = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = local.environment
    },
    {
      ART_RELATIONS_DB_HOST = local.art_relations_db_host
    },
    {
      ART_RELATIONS_DB_USER = local.service_name
    },
    {
      DIRECT_DELIVERY_DB_HOST = local.direct_delivery_db_host
    },
    {
      DIRECT_DELIVERY_DB_USER = local.service_name
    },
    {
      SERVICE_USER_IDENTITY_ID = local.service_user_identity_id
    },
    {
      SERVICE_USER_IDENTITY_UUID = local.service_user_identity_uuid
    },
    {
      SERVICE_USER_PROFILE_ID = local.service_user_profile_id
    },
    {
      SERVICE_USER_PROFILE_UUID = local.service_user_profile_uuid
    },
    {
      SERVICE_USER_PROFILE_TYPE = "ContentProfile",
    },
    {
      SERVICE_USER_ROLES = replace(local.service_name, "-", "_")
    },
    {
      SENTRY_DSN = module.ows_delivery_metadata_sentry_project.sentry_key_dsn_public_output
    },
  ]
}

module "ows_delivery_metadata_secrets" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset([
    "ART_RELATIONS_DB_PASSWORD",
    "DIRECT_DELIVERY_DB_PASSWORD",
  ])

  environment                    = local.environment
  service_name                   = local.service_name
  secret_name                    = each.value
  application_family             = local.application_family
  secret_recovery_window_in_days = 30
}

module "ows_delivery_metadata_m2m_secrets" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//modules/auth0m2m?ref=1.6.1"

  environment        = local.environment
  service_name       = local.service_name
  application_family = local.application_family
}
