resource "aws_elasticache_subnet_group" "this" {
  name        = "${var.project}-${var.environment}-cache-subnet"
  description = "Helix ${var.environment} cache subnets"
  subnet_ids  = var.subnet_ids
}

resource "aws_elasticache_replication_group" "this" {
  replication_group_id = "${var.project}-${var.environment}-redis"
  description          = "Helix ${var.environment} Redis"
  node_type            = var.node_type
  num_cache_clusters   = var.num_cache_clusters
  engine               = "redis"
  engine_version       = "7.1"
  port                 = 6379
  parameter_group_name = "default.redis7"

  subnet_group_name  = aws_elasticache_subnet_group.this.name
  security_group_ids = [var.security_group_id]

  at_rest_encryption_enabled = true
  transit_encryption_enabled = true
  automatic_failover_enabled = var.num_cache_clusters > 1

  tags = {
    Project     = var.project
    Environment = var.environment
  }
}
