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

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/ows-features/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_acm_certificate" "theorchard_io" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

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

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

  providers = {
    aws.dns = aws.networking
  }

  application_family                = var.application_family
  environment                       = var.environment
  service_name                      = var.service_name
  aws_region                        = var.region
  commit_sha                        = "latest"
  container_port                    = "8080"
  task_type                         = "web_service"
  desired_task_count                = 12
  task_cpu                          = 2048
  task_memory                       = 4096
  maximum_capacity                  = 16
  minimum_capacity                  = 8
  route53_zone_id                   = data.aws_route53_zone.route53_zone.zone_id
  health_check_grace_period_seconds = 300
  container_start_period_seconds    = 300

  https_listener_certificate_id = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  http_listener_enabled         = false
  vpc_id                        = module.vpc_info.vpc_id
  fargate_service_subnets       = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets         = module.vpc_info.default_private_subnet_ids


  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/Dynamo-prod-ows-features-owsrequest",
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      LOGGER_LEVEL = var.LOGGER_LEVEL
    },
    {
      REDIS_CACHE_TTL = var.REDIS_CACHE_TTL
    },
    {
      SPLITIO_ENABLED = var.SPLITIO_ENABLED
    },
  ]
}

module "elasticache" {
  # checkov:skip=CKV_AWS_30:PP-1089 Upgrade code to use rediss
  # checkov:skip=CKV_AWS_31:PP-1089 Upgrade code to use rediss
  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=3.1.1"

  providers = {
    aws.dns = aws.networking
  }

  environment                      = "prod"
  application_family               = var.application_family
  service_name                     = "ows-features"
  cache_engine                     = "redis"
  cache_subnet_group_name          = "prod-vpc-orchard-subnet"
  cache_node_type                  = "cache.t4g.micro"
  cache_parameter_group_name       = "default.redis5.0"
  cache_transit_encryption_enabled = false
  vpc_id                           = module.vpc_info.vpc_id
  route53_zone_id                  = data.aws_route53_zone.route53_zone.zone_id
}

module "fargate_service_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.4"
  application_family                = var.application_family
  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  notification_endpoints            = "@slack-permissions-platform-alerts"
  escalation_notification_endpoints = "@slack-permissions-platform-alerts"
  dependent_applications            = var.dependent_applications
  service_cpu_time_window           = "last_15m"
}
