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 backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/vector/gras-delivery/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_caller_identity" "current" {}

data "aws_iam_policy_document" "sns_gras_delivery_policy_document" {
  statement {
    actions = [
      "sns:Publish",
    ]

    resources = [
      module.sns_gras_delivery_topic.topic_arn,
    ]
  }
}

resource "aws_iam_policy" "sns_gras_delivery_policy" {
  name   = "SNS-${var.environment}-${var.service_name}"
  policy = data.aws_iam_policy_document.sns_gras_delivery_policy_document.json

  tags = {
    environment        = var.environment
    service_name       = var.service_name
    application_family = var.application_family
    terraformed        = true
  }
}

data "aws_sqs_queue" "gras_delivery_sqs_queue" {
  name = "${var.environment}-gras_delivery-queue"
}

data "aws_iam_policy" "gras_delivery_sqs_queue_minimal_policy" {
  name = "SQS-${var.environment}-gras_delivery-minimal-policy"
}

module "sns_gras_delivery_topic" {
  source = "git@github.com:theorchard/terraform-sns.git?ref=3.0.0"

  sns_topic_name           = var.service_name
  environment              = var.environment
  application_family       = var.application_family
  sns_subscription_enabled = false
}

# sentry module
module "sentry_gras_delivery" {
  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
  teams              = [var.environment]
  platform           = "php"
}

module "owsreqeust_gras_delivery" {
  source = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.1.0"

  policy_description = "Dynamo-${var.environment}-${var.service_name}-owsrequest"
  environment_name   = var.environment
  service_name       = var.service_name
}

module "gras_delivery_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.7.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"
  health_check_command               = "pgrep php"
  deployment_minimum_healthy_percent = "100"
  autoscaling_cpu_policy_enabled     = false
  autoscaling_memory_policy_enabled  = false
  desired_task_count                 = 0
  minimum_capacity                   = 0
  maximum_capacity                   = 1
  task_cpu                           = 2048
  task_memory                        = 4096
  container_port                     = 8080
  health_check_grace_period_seconds  = 300
  container_start_period_seconds     = 300
  target_deregistration_delay        = 300
  cloudwatch_event_enabled           = true
  cloudwatch_event_schedule          = "rate(1 hour)"
  vpc_id                             = module.vpc_info.vpc_id
  fargate_service_subnets            = module.vpc_info.default_private_subnet_ids
  ows_machine_to_machine_enabled     = true
  splitio_enabled                    = false

  iam_managed_policy_attachments = [
    aws_iam_policy.sns_gras_delivery_policy.arn,
    module.owsreqeust_gras_delivery.policy_arn_output,
    data.aws_iam_policy.gras_delivery_sqs_queue_minimal_policy.arn,
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      AWS_REGION = var.aws_region
    },
    {
      SNS_TOPIC = module.sns_gras_delivery_topic.topic_arn
    },
    {
      SQS_QUEUE_URL = data.aws_sqs_queue.gras_delivery_sqs_queue.url
    },
    {
      GRAS_DOMAIN = var.gras_domain
    },
    {
      GRAS_PORT = var.gras_port
    },
    {
      GRAS_USER = var.gras_user
    },
    {
      AR_DB_HOST = var.ar_db_host
    },
    {
      AR_DB_USER = var.ar_db_user
    },
    {
      DD_DB_HOST = var.dd_db_host
    },
    {
      DD_DB_USER = var.dd_db_user
    },
    {
      PHP_MEMORY_LIMIT = var.php_memory_limit
    },
    {
      PHP_DATE_TIMEZONE = var.php_date_timezone
    },
    {
      SENTRY_DSN = module.sentry_gras_delivery.sentry_key_dsn_public_output
    },
    {
      PUBLIC_KEY = var.gras_public_key
    },
    {
      WEBSERVICES_URL = "https://webservices.theorchard.com"
    }
  ]

  secrets = [
    {
      AR_DB_PASSWORD = "${var.environment}/${var.service_name}/AR_DB_PASSWORD"
    },
    {
      DD_DB_PASSWORD = "${var.environment}/${var.service_name}/DD_DB_PASSWORD"
    },
    {
      PRIVATE_KEY = "${var.environment}/${var.service_name}/PRIVATE_KEY"
    },
  ]
}

module "gras_delivery_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
}

module "gras_delivery_fargate_service_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.6"

  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  application_family                = var.application_family
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.notification_endpoints
  service_5xx_monitor_enabled       = false
  service_4xx_monitor_enabled       = false
  healthy_tasks_monitor_enabled     = false
}

module "m2m_secrets" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//modules/auth0m2m?ref=1.6.1"

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