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.region

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations - of the form ${var.environment}-${var.service_name}-state
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/lambda-vector/throttler-manager/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
}

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

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

data "aws_lambda_function" "lambda_throttler" {
  function_name = "${var.environment}-lambda-vector-throttler"
}

resource "aws_iam_policy" "throttler_manager_task_policy" {
  name   = "${var.environment}-${var.service_name}-task-policy"
  policy = data.aws_iam_policy_document.throttler_manager_task_policy.json
}

data "aws_iam_policy_document" "throttler_manager_task_policy" {
  statement {
    effect = "Allow"
    actions = [
      "lambda:InvokeFunction"
    ]
    resources = [
      data.aws_lambda_function.lambda_throttler.arn,
    ]
  }
}

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

  providers = {
    aws.dns = aws.networking
  }

  environment                    = var.environment
  application_family             = var.application_family
  service_name                   = var.service_name
  autoscaling_cpu_policy_enabled = false
  aws_region                     = var.region
  commit_sha                     = "latest"
  container_port                 = "8080"
  task_type                      = "worker"
  desired_task_count             = 0
  task_cpu                       = 1024
  task_memory                    = 2048
  maximum_capacity               = 1
  minimum_capacity               = 0
  vpc_id                         = module.vpc_info.vpc_id
  cloudwatch_event_enabled       = true
  cloudwatch_event_schedule      = "rate(1 minute)"
  ows_machine_to_machine_enabled = false
  splitio_enabled                = false

  iam_managed_policy_attachments = [
    aws_iam_policy.throttler_manager_task_policy.arn
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      SENTRY_DSN = module.sentry_lambda_throttler_manager.sentry_key_dsn_public_output
    },
    {
      DD_MYSQL_USER = "lambda-throt-mgr"
    },
    {
      DD_MYSQL_HOST = "dd-db.theorchard.com"
    },
    {
      DD_MYSQL_PORT = 3306
    },
    {
      DD_MYSQL_DATABASE = "direct_delivery"
    },
  ]
}

module "fargate_service_dashboard" {
  source                        = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.4"
  environment                   = var.environment
  environment_type              = "fargate"
  application_family            = var.application_family
  service_name                  = var.service_name
  service_4xx_monitor_enabled   = false
  service_5xx_monitor_enabled   = false
  healthy_tasks_monitor_enabled = false
  service_cpu_monitor_enabled   = false

  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
}

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

  environment                    = var.environment
  service_name                   = var.service_name
  secret_name                    = "DD_MYSQL_PASSWORD"
  application_family             = var.application_family
  secret_recovery_window_in_days = 7
}
