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     = "prod/ows-masters-registry/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" "theorchard_io" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

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

module "ows_masters_registry_owsrequest" {
  source           = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.1.0"
  environment_name = var.environment
  service_name     = var.service_name
}

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

module "ows_masters_registry_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_masters_registry_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          = true
  enabled_cloudwatch_logs_exports           = ["audit", "error", "general"]
  vpc_id                                    = module.vpc_info.vpc_id
  route53_zone_id                           = data.aws_route53_zone.route53_zone.zone_id
}

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

  providers = {
    aws.dns = aws.networking
  }
  application_family                       = var.application_family
  environment                              = var.environment
  service_name                             = var.service_name
  aws_region                               = var.aws_region
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 6
  task_cpu                                 = 2048
  task_memory                              = 4096
  maximum_capacity                         = 8
  minimum_capacity                         = 6
  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

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/SQS-prod-ows-masters-celery-full",
    "arn:aws:iam::437795906767:policy/Dynamo-prod-masters_all_service",
    "arn:aws:iam::437795906767:policy/SQS-prod-yt-ownership-RW",
    "arn:aws:iam::437795906767:policy/S3-prod-orcdbucket_masters-registry-reports_bulk-action-RW"
  ]

  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 = [
    "shared-orcd-jump-box-private-subnet-prefix-list",
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      AWS_DEFAULT_REGION = "us-east-1"
    },
    {
      BROKER_URL = "redis://:@${module.elasticache.redis_primary_endpoint_address}:6379"
    },
    {
      CELERY_RESULT_BACKEND = "redis://:@${module.elasticache.redis_primary_endpoint_address}:6379"
    },
    {
      CHECK_CARVEOUTS_ON_FIRST_IMPORT = "1"
    },
    {
      NOT_ADDED_TERRITORIES_WARNING = "1"
    },
    {
      PREFETCH_MULTIPLIER = "4"
    },
    {
      SAVE_BULK_TASK_CONTEXT = "1"
    },
    {
      SHOW_SUB_STORE_CARVED_OUT = "1"
    },
    {
      UPC_MUSIC_VIDEO_VALIDATION = "1"
    },
  ]
}

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
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.notification_endpoints
  service_4xx_monitor_enabled       = true
  service_5xx_monitor_enabled       = true
  critical_number_5xx               = 20
  critical_recovery_number_5xx      = 15
  warning_number_5xx                = 10
  warning_recovery_number_5xx       = 5
  service_cpu_time_window           = "last_10m"
}

module "masters_active_dynamodb_table" {
  source = "git@github.com:theorchard/terraform-dynamodb.git//?ref=3.3.1"

  env                = var.environment
  application_family = var.application_family

  use_custom_table_name = true
  table_name            = "masters_active"
  custom_table_name     = "prod-masters_active"

  use_on_demand = true
  hash_key      = "isrc"
  attribute = [
    { name = "isrc", type = "S" }
  ]
}

module "elasticache" {
  # checkov:skip=CKV_AWS_30:ElastiCache encryption in transit requires client library update.
  # checkov:skip=CKV_AWS_31:ElastiCache encryption in transit requires client library update.
  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=3.1.1"

  providers = {
    aws.dns = aws.networking
  }

  environment                      = var.environment
  service_name                     = var.service_name
  application_family               = var.application_family
  cache_engine                     = "redis"
  cache_subnet_group_name          = "${var.environment}-elasticache-subnet-group"
  cache_node_count                 = 2
  cache_node_type                  = "cache.m6g.large"
  cache_transit_encryption_enabled = false
  cache_parameter_group_name       = "default.redis7"
  redis_engine_version             = "7.0"
  vpc_id                           = module.vpc_info.vpc_id
  route53_zone_id                  = data.aws_route53_zone.route53_zone.zone_id
}

module "ows_masters_registry_rds_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/rds?ref=6.13.4"

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

  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.notification_endpoints
}
