resource "random_password" "password" {
  length  = 20
  special = false
}

resource "helm_release" "rabbitmq" {
  depends_on = [random_password.password, var.wait_on]
  name             = "rabbitmq"
  repository       = "oci://registry-1.docker.io/bitnamicharts"
  chart            = "rabbitmq"
  namespace        = var.namespace
  create_namespace = true
  version          = "15.1.0"

  set_sensitive = [{
    name  = "auth.password"
    value = random_password.password.result
  }]

  set = [{
    name  = "replicaCount"
    value = var.replicas
  },
  {
    name  = "persistence.size"
    value = "4Gi"
  }]

  values = [
    templatefile("${path.module}/values.yaml.tftpl", {
      service_uri  = local.service_uri,
      admin        = var.admin,
      tls          = var.tls,
      ingressClass = var.ingressClass
    })
  ]
}

output "installed" {
  value = true
  depends_on = [helm_release.rabbitmq]
}

output "connection_string" {
  value     = "rabbitmq://user:${random_password.password.result}@rabbitmq-headless.${var.namespace}.svc.cluster.local:5672/"
  sensitive = true
}
