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

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/lambda-product-staging/google-drive-asset-transfer/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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

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_iam_policy_document" "product_staging_bucket_rw" {
  statement {
    effect = "Allow"
    actions = [
      "s3:GetObject",
      "s3:PutObject",
      "s3:ListBucket"
    ]
    resources = [
      data.aws_s3_bucket.product_staging_bucket.arn,
      "${data.aws_s3_bucket.product_staging_bucket.arn}/*"
    ]
  }
}

resource "aws_iam_policy" "product_staging_bucket_rw_policy" {
  name   = "S3-${data.aws_s3_bucket.product_staging_bucket.bucket}-google-drive-asset-transfer-rw"
  policy = data.aws_iam_policy_document.product_staging_bucket_rw.json
}

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

  providers = {
    aws.dns = aws
  }

  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
  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_bucket_rw_policy.arn,
  ]

  environment_variables = [
    {
      ENVIRONMENT = var.environment
    },
    {
      OA_USER = var.oa_user
    },
    {
      IDENTITY_ID = var.identity_id
    },
    {
      PROFILE_ID = var.profile_id
    },
    {
      PROFILE_TYPE = var.profile_type
    }
  ]
}

module "google_drive_asset_transfer_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   = true
  service_cpu_monitor_enabled   = true

  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
}
