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"
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/ows-track/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_acm_certificate" "theorchard_io" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

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

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

  environment_name = var.environment
  service_name     = var.service_name
}

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

  environment_name = var.environment
  service_name     = "${var.service_name}-public"
}

module "ows_track_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
  aws_region                               = var.aws_region
  application_family                       = var.application_family
  commit_sha                               = "latest"
  non_ecr_image                            = "437795906767.dkr.ecr.us-east-1.amazonaws.com/ows-track:latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 10
  task_cpu                                 = 4096
  task_memory                              = 8192
  maximum_capacity                         = 14
  minimum_capacity                         = 10
  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]
  blocking_waf_enabled                     = false
  splitio_enabled                          = true

  # overriding here to add in the NYC office (10.30.0.0/22) subnet
  prod_https_listener_allow_cidr_blocks = [
    "192.168.31.0/24",
    "10.40.0.0/22",
    "10.30.0.0/22",
    "10.10.40.0/22",
  ]

  iam_managed_policy_attachments = [
    module.ows_service_owsrequest.policy_arn_output,
    module.ows_service_public_owsrequest.policy_arn_output,
  ]

  fargate_service_subnets = ["subnet-098b89d0c58c5693a", "subnet-067a6b45b079d7296", "subnet-043d235081c966332"]

  load_balancer_subnets = ["subnet-098b89d0c58c5693a", "subnet-067a6b45b079d7296", "subnet-043d235081c966332"]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      AR_MYSQL_MAJOR_VERSION = "8.0"
    },
    {
      SPLITIO_USE_PREFORKED_INITIALIZATION = var.splitio_preforked
    },
  ]

  secrets = [
    {
      SPLITIO_API_KEY = "${var.environment}/split/API_KEY"
    },
  ]
}

module "ows_track_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
  enabled_cloudwatch_logs_exports           = ["audit", "error", "general"]
  rds_db_subnet_group_name                  = "prod-orchard-private-subnet-group"
  rds_cluster_instance_class                = "db.r6g.large"
  rds_cluster_instance_count                = "1"
  rds_master_username                       = "dummy"
  rds_master_password                       = "dummy"
  rds_availability_zones                    = ["us-east-1a", "us-east-1c"]
  rds_engine_version                        = "8.0.mysql_aurora.3.08.2"
  rds_cluster_instance_parameter_group_name = "default.aurora-mysql8.0"
  custom_kms_key_enabled                    = false
  vpc_id                                    = module.vpc_info.vpc_id
  route53_zone_id                           = data.aws_route53_zone.route53_zone.zone_id

  rds_db_cluster_parameters = [
    # Common parameters from the aurora-mysql80sync cluster parameter group
    {
      name  = "aurora_binlog_replication_max_yield_seconds"
      value = "0"
    },
    {
      name         = "binlog_format"
      value        = "ROW"
      apply_method = "pending-reboot"
    },
    {
      name  = "character_set_client"
      value = "utf8mb4"
    },
    {
      name  = "character_set_database"
      value = "utf8mb4"
    },
    {
      name  = "character_set_connection"
      value = "utf8mb4"
    },
    {
      name  = "character_set_filesystem"
      value = "binary"
    },
    {
      name  = "character_set_results"
      value = "utf8mb4"
    },
    {
      name  = "character_set_server"
      value = "utf8mb4"
    },
    {
      name  = "collation_connection"
      value = "utf8mb4_general_ci"
    },
    {
      name  = "collation_server"
      value = "utf8mb4_general_ci"
    },
    # Parameters specific to this instance
    {
      name  = "max_connections"
      value = "2000"
    }
  ]
}

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
  critical_number_5xx               = var.critical_number_5xx
  critical_recovery_number_5xx      = var.critical_recovery_number_5xx
  warning_number_5xx                = var.warning_number_5xx
  warning_recovery_number_5xx       = var.warning_recovery_number_5xx
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
  additional_tags                   = { service_type : "critical" }
  teams                             = [var.application_family]
  service_4xx_monitor_enabled       = false
  dependent_applications            = var.dependent_applications
}

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
  secret_recovery_window_in_days = 7
}

module "ows_track_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.escalation_notification_endpoints
}

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

  environment        = var.environment
  platform           = "python"
  service_name       = var.service_name
  application_family = var.application_family
  teams              = [var.environment]
}
