terraform {
  required_version = "1.14.4"
  required_providers {
    aws = {
      source = "hashicorp/aws"
    }
  }
}

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  = "orcd-terraform-state"
    key     = "qa/insights/playlists/rds/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
}

module "playlistsdb" {
  source = "git@github.com:theorchard/terraform-rds.git//?ref=8.0.0"

  providers = {
    aws.dns = aws.networking
  }

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

  rds_master_username = "dummy"
  rds_master_password = "dummy123"

  rds_cluster_instance_class                = var.rds_instance_type
  rds_cluster_instance_count                = var.rds_cluster_instance_count
  rds_db_cluster_parameter_group_name       = "default.aurora-postgresql16"
  rds_cluster_instance_parameter_group_name = "default.aurora-postgresql16"
  rds_engine                                = "aurora-postgresql"
  rds_engine_version                        = "16"
  rds_db_subnet_group_name                  = "prod-orchard-private-subnet-group"
  rds_availability_zones                    = ["us-east-1a", "us-east-1c"]
  rds_performance_insights_enabled          = false
  enabled_cloudwatch_logs_exports           = ["postgresql"]

  custom_kms_key_enabled = false

  vpc_id          = module.vpc_info.vpc_id
}

variable "aws_region" {
  type        = string
  description = "The AWS region"
  default     = "us-east-1"
}

variable "environment" {
  type    = string
  default = "qa"
}

variable "service_name" {
  type        = string
  description = "The name of the service."
  default     = "playlistsdb"
}

variable "application_family" {
  type        = string
  description = "The name of the application family."
  default     = "insights"
}

# RDS configuration
variable "rds_instance_type" {
  type        = string
  description = "The instance type for the RDS cluster"
  default     = "db.t4g.medium"
}

variable "rds_cluster_instance_count" {
  type        = number
  description = "The number of instances for the RDS cluster"
  default     = 1
}
