variable "wait_on" {
  type        = any
  description = "Resources to wait on"
  default     = true
}

variable "server_ip" {
  type = string
}

variable "ssh_private_key_path" {
  type = string
}

variable "server_domain" {
  type = string
}

resource "random_password" "encryption_key" {
  length  = 32
  special = false
}

resource "random_password" "jwt_secret" {
  length  = 32
  special = false
}

module "arcane" {
  source = "../quadlet-app"
  wait_on = var.wait_on

  server_ip            = var.server_ip
  ssh_private_key_path = var.ssh_private_key_path

  app_name = "arcane"
  image    = "ghcr.io/ofkm/arcane:latest"
  ports    = ["3552:3552"]
  volumes = ["/opt/storage/data/arcane:/app/data:Z", "/run/user/1001/podman/podman.sock:/var/run/docker.sock:Z"]
  #volumes = ["/opt/storage/data/arcane:/app/data:Z"]

  environment = {
    #DOCKER_HOST = "tcp://localhost:2375"
    APP_URL = "https://${var.server_domain}"
    ENCRYPTION_KEY = random_password.encryption_key.result
    JWT_SECRET = random_password.jwt_secret.result
  }

  haproxy_services = [
    {
      name   = "arcane"
      domain = "${var.server_domain}"
      port   = "3552"
      host   = "127.0.0.1"
      tls    = false
    },
  ]

}

output "app_urls" {
  value = module.arcane.app_urls
}

output "installed" {
  value = true
  depends_on = [module.arcane.installed]
}
