resource "helm_release" "cert-manager-webhook-hetzner" {
  namespace  = var.namespace
  name       = "cert-manager-webhook-hetzner"
  repository = "https://vadimkim.github.io/cert-manager-webhook-hetzner"
  chart      = "cert-manager-webhook-hetzner"

  set = [{
    name  = "groupName"
    value = "acme.${var.tld}"
  }]
}

resource "kubernetes_secret" "hetzner-secret" {
  type = "Opaque"
  depends_on = [var.wait_on]

  metadata {
    name      = "hetzner-secret"
    namespace = var.namespace
  }

  data = {
    "api-key" = var.hetzner_api_token
  }
}


output "solver" {
  value = {
    dns01 = {
      webhook = {
        groupName  = "acme.${var.tld}"
        solverName = "hetzner"
        config = {
          secretName = kubernetes_secret.hetzner-secret.metadata[0].name
          apiUrl     = "https://dns.hetzner.com/api/v1"
        }
      }
    }
  }
}

output "installed" {
  value = true
  depends_on = [helm_release.cert-manager-webhook-hetzner]
}
