variable "hcloud_token" {
  type = string
  sensitive = true
}

variable "hdns_token" {
  type = string
  sensitive = true
}

variable "ssh_public_key_path" {
  description = "Path to SSH public key"
  type        = string
}

variable "ssh_private_key_path" {
  description = "Path to SSH private key"
  type        = string
}

variable "server" {
    default = "mars"
}

variable "server_domain" {
  default = "mars.fourlights.dev"
}

variable "ghcr_username" {}
variable "ghcr_token" {}

resource "null_resource" "is_up" {
  connection {
    type        = "ssh"
    host        = var.server
    user        = "fourlights"
    timeout     = "10m"
    agent       = true
    agent_identity  = var.ssh_private_key_path
  }

  provisioner "remote-exec" {
    inline = [
      "whoami",
    ]
  }
}

module "containers-network" {
  wait_on = null_resource.is_up
  source = "../../quadlets/network"

  server_ip = var.server
  ssh_private_key_path = var.ssh_private_key_path

  name = "containers"
}

module "minio" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/minio"
  server_ip = var.server
  server_domain = var.server_domain
  ssh_private_key_path = var.ssh_private_key_path
  https = true
}

module "valkey" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/valkey"
  server_ip = var.server
  ssh_private_key_path = var.ssh_private_key_path
}

module "affine" {
  wait_on = module.postgres.installed
  source = "../../quadlets/modules/affine"
  server_ip = var.server
  server_domain = var.server_domain
  ssh_private_key_path = var.ssh_private_key_path
  
  # Pass postgres password for database connection
  postgres_password = module.postgres.password
}

module "oci-proxy" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/oci-proxy"
  server_ip = var.server
  ssh_private_key_path = var.ssh_private_key_path
}

module "arcane" {
  wait_on = module.oci-proxy.installed
  source = "../../quadlets/modules/arcane"
  server_domain = var.server_domain
  server_ip = var.server
  ssh_private_key_path = var.ssh_private_key_path
}

module "qdrant" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/qdrant"
  server_ip = var.server
  ssh_private_key_path = var.ssh_private_key_path
  server_domain = var.server_domain
}

module "documentdb" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/documentdb"
  server_ip = var.server
  ssh_private_key_path = var.ssh_private_key_path
}

#module "opensign" {
#  wait_on = module.documentdb.installed
#  source = "../../quadlets/modules/opensign"
#  server_ip = var.server
#  server_domain = var.server_domain
#  ssh_private_key_path = var.ssh_private_key_path
#}

module "postgres" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/postgres"
  server_ip = var.server
  ssh_private_key_path = var.ssh_private_key_path
}

module "rabbitmq" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/rabbitmq"
  server_ip = var.server
  ssh_private_key_path = var.ssh_private_key_path
}

#module "plane" {
#  count = 0
#  wait_on = null_resource.is_up
#  source = "../../quadlets/modules/plane"
#  server_ip = var.server
#  ssh_private_key_path = var.ssh_private_key_path
#  server_domain = var.server_domain
#}

#module "airsonic-advanced" {
#  wait_on = null_resource.is_up
#  source = "../../quadlets/modules/airsonic-advanced"
#  server_ip = var.server
#  ssh_private_key_path = var.ssh_private_key_path
#  server_domain = var.server_domain
#}

module "gonic" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/gonic"
  server_ip = var.server
  server_domain = var.server_domain
  ssh_private_key_path = var.ssh_private_key_path
}

module "tmail-web" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/tmail-web"
  server_ip = var.server
  server_domain = var.server_domain
  ssh_private_key_path = var.ssh_private_key_path
  server_url = "https://mail.binarysunset.dev"
}

module "navidrome" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/navidrome"
  server_ip = var.server
  ssh_private_key_path = var.ssh_private_key_path
  server_domain = var.server_domain
}

module "mopidy" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/mopidy"
  server_ip = var.server
  ssh_private_key_path = var.ssh_private_key_path
  server_domain = var.server_domain
}

module "zot" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/zot"
  server_ip = var.server
  ssh_private_key_path = var.ssh_private_key_path
  server_domain = var.server_domain
}

# Deploy Plane after all dependencies are ready
#module "plane" {
#  wait_on = null_resource.is_up
#
#  source = "../../quadlets/modules/plane"
#  server_ip = var.server
#  server_domain = var.server_domain
#  ssh_private_key_path = var.ssh_private_key_path
#  
#  # Pass credentials from existing services
#  postgres_password = module.postgres.password
#  minio_server = module.minio.server
#  minio_access_key = module.minio.access_key
#  minio_secret_key = module.minio.secret_key
#  rabbitmq_username = module.rabbitmq.username
#  rabbitmq_password = module.rabbitmq.password
#}

module "beets" {
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/beets"
  server_ip = var.server
  server_domain = var.server_domain
  ssh_private_key_path = var.ssh_private_key_path
}

module "deeptutor" {
  count = 0
  wait_on = null_resource.is_up
  source = "../../quadlets/modules/deeptutor"
  server_ip = var.server
  server_domain = var.server_domain
  ssh_private_key_path = var.ssh_private_key_path
}

output "psql_pw" {
  value = module.postgres.password
  sensitive = true
}

module "forgejo" {
  wait_on = module.postgres.installed
  source = "../../quadlets/modules/forgejo"
  server_ip = var.server
  server_domain = var.server_domain
  ssh_private_key_path = var.ssh_private_key_path
  
  # Pass postgres password for database connection
  postgres_password = module.postgres.password
}

output "minio_app_urls" {
  value = module.minio.app_urls
}

output "qdrant_api_key" {
    value = module.qdrant.api_key
    sensitive = true
}

#output "plane_app_urls" {
#  value = module.plane.app_urls
#}
#
#output "plane_credentials" {
#  value = module.plane.credentials
#  sensitive = true
#}
#
#output "plane_main_url" {
#  value = module.plane.main_url
#}

output "forgejo_app_urls" {
  value = module.forgejo.app_urls
}

output "affine_app_urls" {
  value = module.affine.app_urls
}

#output "deeptutor_app_urls" {
#  value = module.deeptutor.app_urls
#}

output "tmail_web_app_urls" {
  value = module.tmail-web.app_urls
}

#output "plane_credentials" {
#value = module.plane.credentials
#sensitive = true
#}