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  = "orcd-terraform-state"
    key     = "qa/maxwells/maxwells-neo4j-kinesis/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

data "aws_kinesis_stream" "qa_neo4j_stream" {
  name = "qa-neo4j-stream"
}

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

data "aws_iam_policy_document" "maxwells_kinesis_policy" {
  statement {
    effect = "Allow"
    actions = [
      "kinesis:PutRecord",
      "kinesis:PutRecords",
      "kinesis:DescribeStream",
      "kinesis:ListShards"
    ]
    resources = [
      data.aws_kinesis_stream.qa_neo4j_stream.arn
    ]
  }

  statement {
    effect = "Allow"
    actions = [
      "cloudwatch:PutMetricData"
    ]
    resources = [
      "*"
    ]
  }
}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git//?ref=3.0.4"

  environment = var.environment
}

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

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

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

  providers = {
    aws.dns = aws.networking
  }

  environment                 = var.environment
  non_ecr_image               = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${var.aws_region}.amazonaws.com/orchard-maxwells-daemon:latest"
  service_name                = var.service_name
  application_family          = var.application_family
  aws_region                  = var.aws_region
  commit_sha                  = "latest"
  container_port              = "8080"
  task_type                   = "worker"
  desired_task_count          = 1
  health_check_command        = "/bin/bash /app/healthcheck.sh"
  task_cpu                    = 2048
  task_memory                 = 4096
  task_ephemeral_storage_size = 100
  maximum_capacity            = 1
  minimum_capacity            = 1
  # Ensure there is only ever one task running during a deployment
  deployment_maximum_percent         = 100
  deployment_minimum_healthy_percent = 0
  vpc_id                             = module.vpc_info.vpc_id
  iam_managed_policy_attachments = [
    aws_iam_policy.maxwells_kinesis_policy.arn
  ]

  fargate_spot_capacity_provider_weight = 100
  task_placement_failure_alert_enabled  = true

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  # Not needed since there's only one task
  availability_zone_rebalancing = "DISABLED"

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      MAXWELL_PRODUCER = var.maxwell_producer
    },
    {
      MAXWELL_MYSQL_HOST = var.maxwell_mysql_host
    },
    {
      MAXWELL_MYSQL_USER = var.maxwell_mysql_user
    },
    {
      INCLUDE_EXPRESSIONS = join(",", var.include_expressions)
    },
    {
      MAXWELL_KINESIS_STREAM = data.aws_kinesis_stream.qa_neo4j_stream.name
    },
    {
      MAXWELL_MYSQL_SCHEMA_DATABASE = var.maxwell_mysql_schema_database
    },
    {
      MAXWELL_PRODUCER_PARTITION_BY = var.maxwell_producer_partition_by
    },
    {
      MAXWELL_PRODUCER_PARTITION_BY_FALLBACK = var.maxwell_producer_partition_by_fallback
    },
    {
      MAXWELL_MYSQL_CLIENT_ID = var.maxwell_mysql_client_id
    },
    {
      MAXWELL_MYSQL_REPLICA_SERVER_ID = var.maxwell_mysql_replica_server_id
    },
    {
      MAXWELL_KPL_AGGREGATIONMAXCOUNT = var.maxwell_kpl_aggregation_max_count
    },
  ]

  secrets = [
    {
      MAXWELL_MYSQL_PASSWORD = "${var.environment}/${var.service_name}/MYSQL_PASSWORD"
    },
  ]
}

module "fargate_service_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service//?ref=6.13.7"
  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
  service_cpu_monitor_silenced      = true
  healthy_tasks_monitor_silenced    = true
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints

  healthy_tasks_warning_number          = 0.2
  healthy_tasks_warning_recovery_number = 1
  healthy_tasks_ok_number               = 1
}
