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  = "qa-supply-chain-terraform-state"
    key     = "qa/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 = var.environment
}

data "aws_ec2_managed_prefix_list" "vector_subnets_prefix_list" {
  name = "prod-orcd-private-subnet-prefix-list"
}

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

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

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

  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
}

module "ows_delivery_metadata_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
  desired_task_count                       = 2
  minimum_capacity                         = 2
  maximum_capacity                         = 8
  task_cpu                                 = 1024
  task_memory                              = 2048
  commit_sha                               = "latest"
  container_port                           = 8080
  task_type                                = "web_service"
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  vpc_id                                   = module.vpc_info.vpc_id
  https_listener_certificate_id            = "2de19cb3-c6b6-4c82-8383-9a9872a7d576"
  http_listener_enabled                    = false
  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 = [
    "prod-orcd-private-subnet-prefix-list",
  ]

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

module "ows_delivery_metadata_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 = 30
}

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

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