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
}

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     = "prod/ows-abacus-event/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"
}

data "aws_iam_policy_document" "abacus_airflow_cli_access_document" {
  statement {
    effect  = "Allow"
    actions = ["airflow:CreateCliToken"]
    resources = [
      "arn:aws:airflow:${var.aws_region}:${data.aws_caller_identity.current.account_id}:environment/${var.environment}-abacus-managed-airflow",
      "arn:aws:airflow:${var.aws_region}:${data.aws_caller_identity.current.account_id}:environment/${var.environment}-abacus-airflow"
    ]
  }
}

# Policy to invoke abacus lambdas
data "aws_iam_policy" "ows_abacus_event_invoke_lambda_policy" {
  arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/${var.environment}-lambda-abacus-generic-invoke-policy"
}


resource "aws_iam_policy" "abacus_airflow_cli_access_policy" {
  name        = "${var.environment}-${var.service_name}-abacus-airflow-cli-access-policy"
  description = "Policy to allow creating cli tokens to trigger dags"
  policy      = data.aws_iam_policy_document.abacus_airflow_cli_access_document.json
}

# Policies to invoke documents lambdas
data "aws_iam_policy" "ows_abacus_event_invoke_lambda_documents_post_to_subledger_policy" {
  arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/${var.environment}-lambda-documents-post-to-subledger-invoke-policy"
}

data "aws_iam_policy" "ows_abacus_event_invoke_lambda_documents_send_payments_policy" {
  arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/${var.environment}-lambda-documents-send-payments-invoke-policy"
}

data "aws_iam_policy" "ows_abacus_event_invoke_lambda_documents_close_balance_policy" {
  arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/${var.environment}-lambda-documents-close-balance-invoke-policy"
}

data "aws_iam_policy" "ows_abacus_event_invoke_lambda_documents_generate_payments_policy" {
  arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/${var.environment}-lambda-documents-generate-payments-invoke-policy"
}

data "aws_iam_policy" "ows_abacus_event_invoke_lambda_documents_calculate_payments_policy" {
  arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/${var.environment}-lambda-documents-calculate-payments-invoke-policy"
}

data "aws_iam_policy" "ows_abacus_event_invoke_lambda_collaborator_auto_report_run_policy" {
  arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/${var.environment}-lambda-collaborator-auto-report-run-invoke-policy"
}

module "ows_abacus_event_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_event_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.6.0"

  providers = {
    aws.dns = aws.networking
  }

  environment                              = var.environment
  service_name                             = var.service_name
  aws_region                               = var.aws_region
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 2
  minimum_capacity                         = 2
  maximum_capacity                         = 4
  task_cpu                                 = 512
  task_memory                              = 1024
  route53_zone_id                          = data.aws_route53_zone.route53_zone.zone_id
  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

  iam_managed_policy_attachments = [
    module.ows_abacus_event_owsrequest.policy_arn_output,
    aws_iam_policy.abacus_airflow_cli_access_policy.arn,
    data.aws_iam_policy.ows_abacus_event_invoke_lambda_policy.arn,
    data.aws_iam_policy.ows_abacus_event_invoke_lambda_documents_post_to_subledger_policy.arn,
    data.aws_iam_policy.ows_abacus_event_invoke_lambda_documents_send_payments_policy.arn,
    data.aws_iam_policy.ows_abacus_event_invoke_lambda_documents_close_balance_policy.arn,
    data.aws_iam_policy.ows_abacus_event_invoke_lambda_documents_generate_payments_policy.arn,
    data.aws_iam_policy.ows_abacus_event_invoke_lambda_documents_calculate_payments_policy.arn,
    data.aws_iam_policy.ows_abacus_event_invoke_lambda_collaborator_auto_report_run_policy.arn,
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

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

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      MYSQL_DB_HOST = var.mysql_db_host
    },
    {
      MYSQL_DB_USER = var.mysql_db_user
    },
    {
      SPLITIO_USE_PREFORKED_INITIALIZATION = "false"
    },
    {
      AWS_AIRFLOW_ENVIRONMENT_NAME = "prod-abacus-airflow"
    }
  ]
}

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

  environment            = var.environment
  environment_type       = "fargate"
  service_name           = var.service_name
  application_family     = var.application_family
  dependent_applications = var.dependent_applications
  teams                  = var.teams

  additional_tags = {
    provisioned_by = "terraform"
  }

  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.notification_endpoints

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

  # Warn for at least 1 event and alert for more than 1 event 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

  # Override notifications for 5xx events
  notification_overrides_5xx = {
    endpoints            = "${var.notification_endpoints}"
    alert_endpoints      = "${var.notification_endpoints} ${var.pagerduty_alert_channel}"
    escalation_endpoints = "${var.notification_endpoints} ${var.pagerduty_alert_channel}"
  }
}

module "sentry-ows-abacus-event" {
  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_event_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
}
