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 = "us-east-1"

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/swf-feed-ingestion/apple-podcasts-subscription-events/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 "fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.6.1"

  providers = {
    aws.dns = aws
  }

  environment                    = var.environment
  service_name                   = var.service_name
  application_family             = var.application_family
  secrets_manager_service_name   = var.service_name
  aws_region                     = var.aws_region
  commit_sha                     = "latest"
  container_port                 = "8080"
  task_type                      = "worker"
  desired_task_count             = 1
  task_cpu                       = 2048
  task_memory                    = 4096
  task_ephemeral_storage_size    = 200
  maximum_capacity               = 1
  minimum_capacity               = 1
  vpc_id                         = module.vpc_info.vpc_id
  iam_managed_policy_attachments = [for policy in data.aws_iam_policy.existing_swf_policies : policy.arn]
  non_ecr_image                  = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/swf-feed-ingestion:latest"

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      AWS_REGION = var.aws_region
    },
    {
      SWF_DOMAIN = local.swf_domain
    },
    {
      REPORTER_JAR = var.reporter_jar
    },
    {
      FEED_INGESTION_DATA_BUCKET = var.feed_ingestion_data_bucket
    },
    {
      FEED_INGESTION_TABLE = local.feed_ingestion_table
    },
    {
      SNOWFLAKE_USER = var.snowflake_user
    },
    {
      SNOWFLAKE_SCHEMA = var.snowflake_schema
    },
    {
      SNOWFLAKE_WAREHOUSE = var.snowflake_warehouse
    },
    {
      SNOWFLAKE_ACCOUNT = var.snowflake_account
    },
    {
      SNOWFLAKE_DATABASE = var.snowflake_database
    },
    {
      SNOWFLAKE_ROLE = var.snowflake_role
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      GARCON_FLOW_NAME = var.flow_name
    },
  ]

  secrets = [
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
    {
      SNOWFLAKE_KEY = "${var.environment}/${var.service_name}/SNOWFLAKE_KEY"
    }
  ]
}

module "sentry_project" {
  source                       = "git@github.com:theorchard/terraform-sentry.git//?ref=4.1.2"
  service_name                 = var.service_name
  application_family           = var.application_family
  secrets_manager_service_name = var.service_name
  environment                  = var.environment
  teams                        = [var.environment]
  platform                     = "python"
}

module "fargate_service_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.4"
  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  application_family                = var.application_family
  service_4xx_monitor_enabled       = false
  service_5xx_monitor_enabled       = false
  service_cpu_monitor_silenced      = true
  healthy_tasks_monitor_silenced    = true
  notification_endpoints            = "@slack-podcast-notifications"
  escalation_notification_endpoints = "@slack-podcast-notifications"
}

module "secrets" {
  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
}

data "aws_iam_policy" "existing_swf_policies" {
  for_each = toset(local.existing_swf_policy_names)
  name     = each.value
}
