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"

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "qa-accounting-terraform-state"
    key     = "uat/ows-ledger/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

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 "vpc" {
  source = "git@github.com:theorchard/terraform-vpc-info.git//?ref=3.1.0"

  environment = var.environment
}

data "aws_route53_zone" "route53_zone" {
  name = "qa-accounting.theorchard.io"
}

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

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

  providers = {
    aws.dns = aws.networking
  }

  environment                   = var.environment
  service_name                  = var.service_name
  aws_region                    = var.aws_region
  desired_task_count            = 0
  minimum_capacity              = 0
  maximum_capacity              = 4
  task_cpu                      = 2048
  task_memory                   = 4096
  route53_zone_id               = data.aws_route53_zone.route53_zone.zone_id
  vpc_id                        = module.vpc.vpc_id
  https_listener_certificate_id = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  application_family            = var.application_family

  iam_managed_policy_attachments = [
    module.ows_ledger_owsrequest.policy_arn_output
  ]

  # checkov:skip=ORCD_AWS_1:Silencing the "Avoid using subnets that are short on available IPs" notification.
  fargate_service_subnets = module.vpc.default_private_subnet_ids

  load_balancer_subnets = module.vpc.default_private_subnet_ids

  https_listener_allow_prefix_list_names = [
    "shared-orcd-jump-box-private-subnet-prefix-list",
  ]

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

  secrets = [
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
    {
      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.13.7"
  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
  additional_tags                   = { service_type = "critical" }
  teams                             = ["abacus-qa"]
  healthy_tasks_monitor_enabled     = true
  failed_tasks_monitor_enabled      = true
  dependent_applications            = var.dependent_applications

  # Warn for at least 1 event and alert for more than 1 event over a 5m window
  service_5xx_monitor_enabled  = true
  warning_number_5xx           = 0.1
  warning_recovery_number_5xx  = 0
  critical_number_5xx          = 1
  critical_recovery_number_5xx = 0.1
  ok_number_5xx                = 0

  # Warn for 10 events or more and alert for 20 events or more over a 5m window
  service_4xx_monitor_enabled  = true
  warning_number_4xx           = 10
  warning_recovery_number_4xx  = 0
  critical_number_4xx          = 20
  critical_recovery_number_4xx = 10
  ok_number_4xx                = 0
}

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

  environment        = var.environment
  platform           = "python"
  service_name       = "ows-ledger"
  teams              = ["abacus-qa"]
  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
}
