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/prs-toolkit/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 "cloudflare_zone" "cloudflare_cloudflare_zone" {
  name = var.domain_name
}

data "aws_route53_zone" "theorchard_com" {
  name         = "theorchard.com"
  private_zone = true
}

data "aws_route53_zone" "theorchard_io" {
  name         = "theorchard.io"
  private_zone = false
}

module "prs_toolkit_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"
  task_type                                = "web_service"
  desired_task_count                       = 2
  minimum_capacity                         = 2
  maximum_capacity                         = 2
  task_cpu                                 = 2048
  task_memory                              = 4096
  container_port                           = 80
  health_check_path                        = "/healthchk.php"
  route53_zone_id                          = data.aws_route53_zone.theorchard_io.zone_id
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  load_balancer_idle_timeout               = 3600
  http_listener_enabled                    = false
  vpc_id                                   = module.vpc_info.vpc_id

  iam_managed_policy_attachments = [
    module.efs_volume.efs_iam_policy_arn_output,
  ]
  docker_volumes = [
    {
      name            = "${var.environment}-${var.service_name}"
      file_system_id  = module.efs_volume.efs_file_system_id_output
      access_point_id = module.efs_volume.efs_access_point_id_output
    }
  ]

  docker_volume_mount_points = [
    {
      source_volume  = "${var.environment}-${var.service_name}"
      container_path = var.efs_volume_container_path
    }
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets   = module.vpc_info.default_private_subnet_ids

  prod_https_listener_allow_cidr_blocks = [
    "192.168.30.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.37.0/27", # statement processing users
    "192.168.40.0/22",
    "10.30.0.0/22",
    "10.40.0.0/22",
  ]

  https_listener_allow_prefix_list_names = [
    "vpn-sme-internal-primary",
  ]

  environment_variables = var.prs_toolkit_env_vars

  secrets = [
    {
      ASSIST_PASSWORD = "${var.environment}/${var.service_name}/ASSIST_PASSWORD"
    },
    {
      RESEARCH_PASSWORD = "${var.environment}/${var.service_name}/RESEARCH_PASSWORD"
    },
    {
      PPB_PASSWORD = "${var.environment}/${var.service_name}/PPB_PASSWORD"
    },
    {
      RA_PASSWORD = "${var.environment}/${var.service_name}/RA_PASSWORD"
    }
  ]
}

module "prs_toolkit_secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.secrets_manager_secret_names)

  application_family = "security-tools"
  environment        = var.environment
  service_name       = var.service_name
  secret_name        = each.value
}

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            = "@slack-devops" #temp
  escalation_notification_endpoints = "@slack-devops" #temp
}
