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/split-synchronizer/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 "elasticache" {
  # checkov:skip=CKV_AWS_30:ElastiCache encryption in transit requires client library update.
  # checkov:skip=CKV_AWS_31:ElastiCache encryption in transit requires client library update.
  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=3.1.1"

  providers = {
    aws.dns = aws.networking
  }

  environment                      = var.environment
  service_name                     = var.service_name
  cache_engine                     = "redis"
  cache_subnet_group_name          = "prod-vpc-orchard-subnet"
  cache_node_type                  = "cache.t4g.micro"
  cache_node_count                 = 2
  cache_parameter_group_name       = "default.redis5.0"
  redis_engine_version             = "5.0.6"
  vpc_id                           = module.vpc_info.vpc_id
  route53_zone_id                  = data.aws_route53_zone.route53_zone.zone_id
  application_family               = var.application_family
  additional_cidr_blocks_enabled   = true
  cache_transit_encryption_enabled = false
  additional_cidr_blocks = [
    "10.10.40.0/22",
    "10.40.0.0/22",
    "10.30.0.0/22",
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
  ]
}

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

  providers = {
    aws.dns = aws.networking
  }

  environment        = var.environment
  service_name       = var.service_name
  aws_region         = var.region
  commit_sha         = "latest"
  container_port     = "3010"
  task_type          = "web_service"
  desired_task_count = 1
  task_cpu           = 256
  task_memory        = 512
  maximum_capacity   = 1
  minimum_capacity   = 1
  application_family = var.application_family
  route53_zone_id    = data.aws_route53_zone.route53_zone.zone_id

  vpc_id                        = module.vpc_info.vpc_id
  https_listener_certificate_id = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  health_check_path             = "/admin/ping"

  iam_managed_policy_attachments = [
    "arn:aws:iam::aws:policy/CloudWatchFullAccess"
  ]

  prod_https_listener_allow_cidr_blocks = [
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
    "10.40.0.0/22",
    "10.30.0.0/22",
    "10.10.40.0/22"
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      SPLIT_SYNC_REDIS_HOST = module.elasticache.redis_primary_endpoint_address
    },
    {
      SPLIT_SYNC_REDIS_PORT = "6379"
    },
    {
      SPLIT_SYNC_ADVANCED_PARAMETERS = "-config /var/app/config.json"
    },
    {
      SPLIT_SYNC_LOG_DEBUG = "on"
    },
    {
      SPLIT_SYNC_LOG_STDOUT = "on"
    },
    {
      SPLIT_SYNC_LOG_VERBOSE = "on"
    }
  ]

  secrets = [
    {
      SPLIT_SYNC_API_KEY = "${var.environment}/${var.service_name}/SPLIT_SYNC_API_KEY"
    }
  ]
}
