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-accounting-terraform-state"
    key     = "qa/ows-abacus-account/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_route53_zone" "route53_zone" {
  provider = aws.networking

  name         = "theorchard.io."
  private_zone = false
}

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

module "service_info" {
  for_each     = toset(local.downstream_services)
  source       = "git@github.com:theorchard/terraform-service-info.git//?ref=1.0.0"
  service_name = each.value
  environment  = var.environment
}

module "lambda_function_info" {
  for_each            = toset(local.downstream_lambda_functions)
  source              = "git@github.com:theorchard/terraform-service-info.git//?ref=1.0.0"
  service_name        = each.value
  security_group_type = "lambda"
  environment         = var.environment
}

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

  environment_name = var.environment
  service_name     = var.service_name
}

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

  providers = {
    aws.dns = aws.networking
  }

  environment                   = var.environment
  service_name                  = var.service_name
  aws_region                    = var.aws_region
  desired_task_count            = 6
  minimum_capacity              = 6
  maximum_capacity              = 6
  task_cpu                      = 512
  task_memory                   = 1024
  vpc_id                        = module.vpc_info.vpc_id
  https_listener_certificate_id = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  blocking_waf_enabled          = true
  application_family            = var.application_family

  https_listener_allow_security_group_ids = flatten([
    [for service in module.service_info : service.security_groups],
    [for lambda in module.lambda_function_info : lambda.security_groups],
  ])

  https_listener_allow_prefix_list_names = [
    "qa-orcd-private-subnet-prefix-list", # Allow access from qa-ows-coda (main QA account)
  ]

  iam_managed_policy_attachments = [
    module.ows_abacus_account_owsrequest.policy_arn_output
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      DD_REMOTE_CONFIGURATION_ENABLED = "false"
    },
    {
      MYSQL_DB_HOST = var.mysql_db_host
    },
    {
      MYSQL_DB_USER = var.mysql_db_user
    },
    {
      SCHEMA_REGISTRY_URL = var.schema_registry_url
    },
    {
      KAFKA_BOOTSTRAP_SERVERS = var.kafka_bootstrap_servers
    },
    {
      SPLITIO_USE_PREFORKED_INITIALIZATION = "false"
    }
  ]
}

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

  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
}

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

  environment        = var.environment
  platform           = "python"
  service_name       = var.service_name
  teams              = ["abacus-${var.environment}"]
  application_family = var.application_family
}

module "ows_abacus_account_secrets" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"

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