module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=1.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
}

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  = "qa-fansifter-terraform-state"
    key     = "qa/preference-center-web/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_ec2_managed_prefix_list" "cloudfront" {
  name = "com.amazonaws.global.cloudfront.origin-facing"
}

data "aws_route53_zone" "route53_zone" {
  name = local.route53_zone
}

data "aws_route53_zone" "theorchard_io_route53_zone" {
  provider = aws.networking
  name     = "theorchard.io."
}

data "aws_iam_policy" "refresh_m2m_token_read_policy" {
  name = "SecretsManager-${var.environment}-refresh-m2m-token-read-policy"
}


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

  providers = {
    aws.dns = aws.networking
  }

  environment        = var.environment
  service_name       = var.service_name
  aws_region         = var.aws_region
  application_family = var.application_family

  desired_task_count                       = 2
  minimum_capacity                         = 2
  maximum_capacity                         = 4
  task_cpu                                 = 1024
  task_memory                              = 2048
  route53_zone_id                          = data.aws_route53_zone.route53_zone.zone_id
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  vpc_id                                   = module.vpc_info.vpc_id
  fargate_service_subnets                  = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets                    = module.vpc_info.default_public_subnet_ids
  load_balancer_is_internal                = false

  health_check_path = "/en/hello"

  splitio_enabled                = false
  ows_machine_to_machine_enabled = false

  iam_managed_policy_attachments = [
    data.aws_iam_policy.refresh_m2m_token_read_policy.arn,
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      HOSTNAME = "0.0.0.0"
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      SENTRY_DSN = module.sentry.sentry_key_dsn_public_output
    },
    {
      OWS_PREFERENCE_CENTER_URL = "https://${var.environment}-ows-preference-center.theorchard.io/"
    }
  ]

  secrets = []
}

resource "aws_security_group_rule" "allow_access_to_lb_from_cloudfront" {
  description       = "allow_https_access_from_cloudfront"
  type              = "ingress"
  security_group_id = module.service_fargate_environment.fargate_load_balancer_security_group_id
  from_port         = 443
  to_port           = 443
  protocol          = "tcp"
  prefix_list_ids   = [data.aws_ec2_managed_prefix_list.cloudfront.id]
}

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

  environment        = var.environment
  environment_type   = "fargate"
  service_name       = var.service_name
  application_family = var.application_family
  teams              = [var.application_family]

  service_cpu_monitor_evaluation_function = "avg"
  service_cpu_time_window                 = "last_10m"
  service_4xx_monitor_enabled             = false

  notification_endpoints            = "@slack-fansifter-alerts"
  escalation_notification_endpoints = "@slack-fansifter"
}

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

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