locals {
  tags = {
    Environment = var.stage
    Project     = var.client_name
    ManagedBy   = "terraform"
    Terraform   = "true"
    env         = "backup"
    aws-apn-id  = "pc:bolb4smnwxqsn7mtfkperljjx"
  }

  db = [
    {
      name                  = "${var.client_name}-${var.stage}-postgres"
      create_db_instance    = true
      type                  = "db.t3.xlarge"
      engine                = "postgres"
      family                = "postgres15"
      engine_version        = "15" # major only — allows AWS auto minor upgrades without Terraform downgrades
      port                  = 5432
      username              = "postgres"
      db_name               = "appdb"
      allocated_storage     = 100
      max_allocated_storage = 500

      backup_window                = "00:00-01:00"
      maintenance_window           = "Mon:01:00-Mon:02:00"
      multi_az                     = true
      performance_insights_enabled = true
      backup_retention_period      = 30
      log_group_retention_in_days  = 365
      subnet_ids                   = slice(var.database_subnets, 0, 2)
      security_group_ids           = [var.db_private_sg_id]
      parameters                   = []
      alerts_topic_arn             = module.baseline.technical_alerts_topic
    }
  ]

  lambda = [
    {
      name                        = "${var.client_name}-${var.stage}-detection"
      description                 = "ML image detection Lambda function"
      runtime                     = "python3.12"
      memory_size                 = 3008
      timeout                     = 900
      log_group_retention_in_days = 365
      subnet_ids                  = slice(var.private_subnets, 6, 8)
      security_group_ids          = [var.lambda_private_sg_id]
      environment_variables = {
        ML_BUCKET_NAME = "${var.client_name}-${var.stage}-ml-image-scan"
      }
      alerts_topic_arn = module.baseline.technical_alerts_topic
    },
  ]

  s3_buckets = {
    ml-image-scan = {
      name = "${var.client_name}-${var.stage}-ml-image-scan"
    }
    supabase-storage = {
      name = "${var.client_name}-${var.stage}-supabase-storage"
    }
  }

  rds_proxy = {
    postgres = {
      name                   = "${var.client_name}-${var.stage}-rds-proxy"
      engine_family          = "POSTGRESQL"
      db_instance_identifier = module.rds-db.db_instance_identifiers["${var.client_name}-${var.stage}-postgres"]
      secret_arn             = module.rds-db.db_instance_master_user_secret_arns["${var.client_name}-${var.stage}-postgres"]
      subnet_ids             = var.database_subnets
      security_group_ids     = [var.rds_proxy_sg_id]
    }
  }

  ec2 = [
    {
      name                = "datafy-prod-supabase-server"
      type                = "t3.xlarge"
      ami                 = data.aws_ami.ubuntu.id
      port                = 8000
      subnet_id           = var.private_subnets[0]
      vpc_zone_subnet_ids = slice(var.private_subnets, 0, 2)
      volume_size         = 100
      create_asg          = false
      desired_capacity    = 1
      min_size            = 1
      max_size            = 1
      security_group_ids  = [var.ec2_private_sg_id]
      target_group_arns   = []
      user_data           = filebase64("${path.module}/../../modules/aws-so-ec2/user_data.txt")
      alerts_topic_arn    = module.baseline.technical_alerts_topic
      health_check_matcher = "200,401"
    },
    {
      name                = "water-wheel-supabase-server"
      type                = "c7i.2xlarge"
      ami                 = data.aws_ami.ubuntu.id
      port                = 80
      subnet_id           = var.private_subnets[1]
      vpc_zone_subnet_ids = slice(var.private_subnets, 0, 2)
      volume_size         = 60
      create_asg          = false
      desired_capacity    = 1
      min_size            = 1
      max_size            = 1
      security_group_ids  = [var.ec2_private_sg_id]
      target_group_arns   = []
      user_data           = filebase64("${path.module}/../../modules/aws-so-ec2/user_data.txt")
      alerts_topic_arn    = module.baseline.technical_alerts_topic
      health_check_path   = "/.well-known/alb-health"
    },
        {
      name                = "qwen3-supabase-server"
      type                = "g4dn.xlarge"
      ami                 = data.aws_ami.ubuntu.id
      port                = 80
      subnet_id           = var.private_subnets[1]
      vpc_zone_subnet_ids = slice(var.private_subnets, 0, 2)
      volume_size         = 50
      create_asg          = false
      desired_capacity    = 1
      min_size            = 1
      max_size            = 1
      security_group_ids  = [var.ec2_private_sg_id]
      target_group_arns   = []
      user_data           = filebase64("${path.module}/../../modules/aws-so-ec2/user_data.txt")
      alerts_topic_arn    = module.baseline.technical_alerts_topic
    },
  ]
}
