locals {

  # storage
  persistent_folder = "/srv"

  lab_domain      = "lab.alexpires.me"
  open_webui_fqdn = "open-webui.${local.lab_domain}"
  internal_issuer = "internal-ca"

  # samba
  samba_shared_folder = "/mnt/usb"

  # DNS
  dns_nameservers = [
    "193.110.81.0",
    "185.253.5.0"
  ]

  override_hosts = [
    {
      name = "cloud.alexpires.me"
      ip   = "10.19.4.136"
    },
  ]

  zones = [
    {
      fqdn = "${local.lab_domain}"
      records = [
        {
          name    = "vh-01"
          type    = "A"
          content = "10.19.4.109"
        },
        {
          name    = "gpu-01"
          type    = "A"
          content = "10.19.4.106"
        },
        {
          name    = "dev-01"
          type    = "A"
          content = "10.19.4.136"
        },
        {
          name    = "ollama"
          type    = "CNAME"
          content = "dev-01.${local.lab_domain}."
        },
        {
          name    = "kokoro"
          type    = "CNAME"
          content = "gpu-01.${local.lab_domain}."
        },
        {
          name    = "open-webui"
          type    = "CNAME"
          content = "dev-01.${local.lab_domain}."
        },
        {
          name    = "comfyui-backend"
          type    = "CNAME"
          content = "gpu-01.${local.lab_domain}."
        },
        {
          name    = "comfyui"
          type    = "CNAME"
          content = "dev-01.${local.lab_domain}."
        },
        {
          name    = "smb"
          type    = "CNAME"
          content = "dev-01.${local.lab_domain}."
        },
        {
          name    = "grafana"
          type    = "CNAME"
          content = "dev-01.${local.lab_domain}."
        },
        {
          name    = "prometheus"
          type    = "CNAME"
          content = "dev-01.${local.lab_domain}."
        },
        {
          name    = "game-01"
          type    = "A"
          content = "10.19.4.245"
        },
        {
          name    = "prod-01"
          type    = "A"
          content = "10.5.5.2"
        },
      ]
    }
  ]

  proxy_services = {
    "comfyui" = {
      http_port  = 8080
      https_port = 8443
      tls        = true
      fqdn       = ["comfyui.lab.alexpires.me"]
      upstream   = "http://upstream.comfyui.svc.cluster.local:8188"
      resolver   = "kube-dns.kube-system.svc.cluster.local"

      locations = [
        {
          path         = "/"
          "Upgrade"    = "$http_upgrade"
          "Connection" = "upgrade"
        },
      ]
    }
  }

  streams = {
    "dns" = {
      port      = 53
      host_port = 53
      upstream  = "dns.dns.svc.cluster.local"
      protocol  = "udp"
    }
  }

  ollama_proxy = {
    enabled = true
    mac     = "04:7c:16:d6:63:66"
    ip      = "10.19.4.106"
    port    = "11434"
    scheme  = "http"
  }

  comfyui_proxy = {
    mac    = "04:7c:16:d6:63:66"
    ip     = "10.19.4.106"
    port   = "8188"
    scheme = "http"
  }

  scaleway_smtp_host = "smtp.tem.scw.cloud"

  nextcloud_fqdn             = "cloud.alexpires.me"
  nextcloud_db_passwd        = var.nextcloud_db_passwd
  nextcloud_db_root_password = var.nextcloud_db_root_password
  nextcloud_db_path          = "${local.persistent_folder}/mysql/nextcloud"
  backup_folder              = "/mnt/usb/backups/nextcloud"
  nextcloud_config = {
    mail_from      = "cloud"
    mail_domain    = "alexpires.me"
    smtp_user      = var.scaleway_project_id
    smtp_password  = local.mail_app_api_secret_key
    smtp_host      = local.scaleway_smtp_host
    admin_password = var.nextcloud_admin_password
    db_host        = module.nextcloud_db.host
    db_name        = module.nextcloud_db.db_name
    db_user        = module.nextcloud_db.app_crendentials_name
    db_password    = module.nextcloud_db.app_crendentials_name
  }

  backup_retention_days = 3
  backup_cron_schedule  = "0 2 * * *" # Daily at 2 AM

  backup_bucket_name        = data.terraform_remote_state.scaleway.outputs.backup_bucket_name
  backup_app_api_secret_key = data.terraform_remote_state.scaleway.outputs.backup_app_api_secret_key
  backup_app_api_access_key = data.terraform_remote_state.scaleway.outputs.backup_app_api_access_key
  mail_app_api_secret_key   = data.terraform_remote_state.scaleway.outputs.mail_app_api_secret_key

  grafana_fqdn = "grafana.${local.lab_domain}"

  remote_targets = {
    "prometheus" = {
      listen_port = 9090
      target_host = "10.5.5.5"
      target_port = 9090
      protocol    = "TCP"
    }
  }
}

data "terraform_remote_state" "scaleway" {
  backend = "s3"
  config = {
    bucket = "a13labs.infra.eu"
    key    = "scaleway.core.infra.tfstate"
    region = "eu-west-1"
  }
}
