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  = "orcd-terraform-state"
    key     = "qa/ows-ledger/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 "lambda_function_info" {
  for_each            = toset(local.downstream_lambda_functions)
  source              = "git@github.com:theorchard/terraform-service-info.git//?ref=1.1.0"
  service_name        = each.value
  security_group_type = "lambda"
  environment         = var.environment
}

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

  environment_name = var.environment
  service_name     = var.service_name
}

module "ows_ledger_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
  desired_task_count                                                     = 2
  minimum_capacity                                                       = 2
  maximum_capacity                                                       = 8
  task_cpu                                                               = 2048
  task_memory                                                            = 4096
  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]
  application_family                                                     = var.application_family
  autoscaling_active_connections_policy_enabled                          = true
  autoscaling_cpu_policy_enabled                                         = false
  scaling_active_connection_count_scale_out_threshold                    = 25
  scaling_active_connection_count_scale_out_number_of_evaluation_periods = 1
  scaling_active_connection_count_scale_in_threshold                     = 5

  iam_managed_policy_attachments = [
    module.ows_ledger_owsrequest.policy_arn_output
  ]

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

  https_listener_allow_prefix_list_names = [
    "qa-accounting-private-subnet-prefix-list",
  ]

  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
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      DD_REMOTE_CONFIGURATION_ENABLED = "false"
    },
    {
      MYSQL_DB_HOST = var.my_sql_db_host
    },
    {
      MYSQL_DB_USER = var.my_sql_db_user
    },
    {
      SPLITIO_USE_PREFORKED_INITIALIZATION = "false"
    },
    {
      UWSGI_WORKERS = var.UWSGI_WORKERS
    },
    {
      UWSGI_CHEAPER_RSS_LIMIT_SOFT = var.UWSGI_CHEAPER_RSS_LIMIT_SOFT
    },
    {
      SENTRY_DSN = module.sentry-ows-ledger.sentry_key_dsn_public_output
    },
  ]

  secrets = [
    {
      MYSQL_DB_PASS = "${var.environment}/${var.service_name}/MYSQL_DB_PASS"
    }
  ]
}

module "ows_ledger_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.notification_endpoints
}

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

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

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

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