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          = "content-creation-management"
}

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/lambda-product-staging/validate-spreadsheet/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
}

data "aws_route53_zone" "route53_zone" {
  name = "theorchard.io"
}

data "aws_s3_bucket" "product_staging_bucket" {
  bucket = "${var.environment}-ows-product-staging"
}

data "aws_iam_policy_document" "product_staging_validate_spreadsheet_bucket_rw_access_document" {
  statement {
    effect = "Allow"
    actions = [
      "s3:GetObject",
      "s3:PutObject"
    ]
    resources = [
      "${data.aws_s3_bucket.product_staging_bucket.arn}/*",
    ]
  }
}

data "aws_iam_policy_document" "product_staging_validate_spreadsheet_task_role_access_document" {
  statement {
    effect = "Allow"
    actions = [
      "states:SendTaskSuccess",
      "states:SendTaskFailure",
      "states:SendTaskHeartbeat",
    ]
    resources = ["*"]
  }
}

resource "aws_iam_policy" "product_staging_validate_spreadsheet_bucket_rw_policy" {
  name   = "S3-${data.aws_s3_bucket.product_staging_bucket.bucket}-validate-spreadsheet-RW"
  policy = data.aws_iam_policy_document.product_staging_validate_spreadsheet_bucket_rw_access_document.json
}

resource "aws_iam_policy" "product_staging_validate_spreadsheet_task_role_policy" {
  name   = "S3-${data.aws_s3_bucket.product_staging_bucket.bucket}-validate-spreadsheet-task-role"
  policy = data.aws_iam_policy_document.product_staging_validate_spreadsheet_task_role_access_document.json
}

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

  environment        = var.environment
  platform           = "python"
  service_name       = var.service_name
  application_family = var.application_family
}

module "fargate_validate_spreadsheet" {
    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
    application_family      = var.application_family

    task_type               = "worker"
    desired_task_count      = 0
    minimum_capacity        = 0
    maximum_capacity        = 0
    task_cpu                = var.task_cpu_size
    task_memory             = var.task_memory_size
    datadog_enabled         = true
    datadog_task_memory     = 512
    datadog_task_cpu        = 256
    commit_sha              = "latest"
    non_ecr_image           = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/${var.task_name}:latest"
    stopped_task_monitoring_enabled = true

    vpc_id                  = module.vpc_info.vpc_id
    route53_zone_id         = data.aws_route53_zone.route53_zone.zone_id
    fargate_service_subnets = module.vpc_info.default_private_subnet_ids
    load_balancer_subnets   = module.vpc_info.default_private_subnet_ids

    health_check_command    = "pgrep python"

    iam_managed_policy_attachments = [
      aws_iam_policy.product_staging_validate_spreadsheet_bucket_rw_policy.arn,
      aws_iam_policy.product_staging_validate_spreadsheet_task_role_policy.arn
    ]

    environment_variables = [
      {
        ENVIRONMENT = var.environment
      },
      {
        IDENTITY_ID = var.identity_id
      },
      {
        PROFILE_ID = var.profile_id
      },
      {
        PROFILE_TYPE = var.profile_type
      },
      {
        SENTRY_DSN = module.product_staging_validate_spreadsheet_sentry_project.sentry_key_dsn_public_output
      }
    ]
}

module "validate_spreadsheet_fargate_service_dashboard" {
  source             = "git@github.com:theorchard/terraform-datadog.git//modules/service//?ref=6.13.6"
  environment        = var.environment
  environment_type   = "fargate"
  application_family = var.application_family
  service_name       = var.service_name

  healthy_tasks_monitor_enabled = false
  service_4xx_monitor_enabled   = false
  service_5xx_monitor_enabled   = false

  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
}
