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
  }
}

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/ows-preference-center/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 = "${var.environment}-fansifter.theorchard.io"
}

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

data "aws_ec2_managed_prefix_list" "orcd_prefix_list" {
  name = "prod-orcd-private-subnet-prefix-list"
}


module "ows_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_private_subnet_ids

  splitio_enabled                = false
  ows_machine_to_machine_enabled = false

  iam_managed_policy_attachments = []

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      SENTRY_DSN = module.sentry.sentry_key_dsn_public_output
    },
    {
      REDIS_HOST = module.redis_elasticache.redis_primary_endpoint_address
    },
    {
      SNOWFLAKE_ACCOUNT = var.snowflake_account
    },
    {
      SNOWFLAKE_DATABASE = var.snowflake_database
    },
    {
      SNOWFLAKE_SCHEMA = var.snowflake_schema
    },
    {
      SNOWFLAKE_WAREHOUSE = var.snowflake_warehouse
    },
    {
      SNOWFLAKE_ROLE = var.snowflake_role
    },
    {
      SNOWFLAKE_USER = var.snowflake_user
    },
    {
      KAFKA_BOOTSTRAP_SERVERS = join(",", var.kafka_bootstrap_servers)
    },
  ]

  secrets = [
    {
      SNOWFLAKE_PASSWORD = "${var.environment}/${var.service_name}/SNOWFLAKE_PASSWORD"
    },
    {
      SNOWFLAKE_PRIVATE_KEY = "${var.environment}/${var.service_name}/SNOWFLAKE_PRIVATE_KEY"
    },
    {
      SNOWFLAKE_KEY_PASSPHRASE = "${var.environment}/${var.service_name}/SNOWFLAKE_KEY_PASSPHRASE"
    },
    {
      SECRET_KEY = "${var.environment}/${var.service_name}/SECRET_KEY"
    },
  ]
}

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           = "python"
  service_name       = var.service_name
  application_family = var.application_family
}

module "redis_elasticache" {
  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=3.1.1"

  providers = {
    aws.dns = aws
  }

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

  vpc_id          = module.vpc_info.vpc_id
  route53_zone_id = data.aws_route53_zone.route53_zone.id

  cache_engine               = "redis"
  redis_engine_version       = "7.0"
  cache_parameter_group_name = "default.redis7"
  cache_node_type            = "cache.t4g.small"
  cache_node_count           = 1
  cache_subnet_group_name    = "${var.environment}-elasticache-subnet-group"
}

module "secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.secrets_manager_secret_names)

  application_family = var.application_family
  environment        = var.environment
  service_name       = var.service_name
  secret_name        = each.value
}

resource "aws_security_group_rule" "allow_orcd_prod_private_ingress" {
  type              = "ingress"
  from_port         = 443
  to_port           = 443
  protocol          = "TCP"
  security_group_id = module.ows_service_fargate_environment.fargate_load_balancer_security_group_id
  prefix_list_ids = [
    data.aws_ec2_managed_prefix_list.orcd_prefix_list.id
  ]
  depends_on = [module.ows_service_fargate_environment]
}
