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 "domain" {
  type    = string
  default = "hub.visualworkplace.fourlights.dev"
}
variable "name" {
  type    = string
  default = "visualworkplace-hub"
}

variable "s3_access_key" {
  type = string
}

variable "s3_secret_key" {
  type = string
}

variable "s3_server" {
  type = string
}

variable "valkey_host" {
  type = string
  default = "systemd-valkey"
}

variable "valkey_db" {
  type = number
  default = 0
}

module "s3-tenant" {
  source     = "../minio/tenant"
  wait_on = var.wait_on

  access_key = var.s3_access_key
  secret_key = var.s3_secret_key
  server     = var.s3_server
  name       = var.name
}

module "vw-hub" {
  source     = "../quadlet-app"
  wait_on = module.s3-tenant.secret_key

  server_ip            = var.server_ip
  ssh_private_key_path = var.ssh_private_key_path

  app_name = var.name
  image    = "ghcr.io/four-lights-nl/vw-hub:8edae556b9c64fb602b8a54e67c3d06656c4bb9e"
  volumes  = ["/opt/storage/data/vw-hub:/run/secrets:Z"]
  ports    = [
    "3000:3000",
  ]

  environment = {
    NODE_ENV                   = "production"
    LOG_LEVEL                  = "info"
    OTEL_LOG_LEVEL             = "info"
    HOST                       = "0.0.0.0"
    PORT                       = "3000"
    OAUTH_CLIENT_ID            = var.name
    OAUTH_CLIENT_SECRET        = "OGZ0IDpkWOJXaFQOr6mbIF7.l0rZLvxQDZPEGv6qHLLH/stP5vAIqHLZ2x05uQn9TFQHtsPkRysGM.RpKlWra0"
    OAUTH_DOMAIN               = "https://${var.domain}"
    BASE_URL                   = "https://${var.domain}"
    REDIS_HOST                 = var.valkey_host
    REDIS_DB                   = var.valkey_db
    KEYS_MASTER_KEY            = "54dd59c1f1c94795a2b63b074a3943674e964b0225e58b7595762d237d9fdcda"
    TOKEN_ENCRYPTION_KEY       = "4d15791e50874fbe8af1a8d0fe2605d65bcf44737b7c36d9b2f99ec3367276c5"
    ZOHO_CLIENT_ID             = "1000.LFYZSCTUJLMUNUUBZX5PMYUXM6HOMP"
    ZOHO_CLIENT_SECRET         = "07093529734781706356ec4bb8ce7274f1df25cb2e"
    ZOHO_REFRESH_TOKEN         = "1000.0808eabe967955a24d403eabec6c0aa5.44fbbd0c6e98c476c6bb7bee70317f82"
    ZOHO_ACCESS_TOKEN          = ""
    ZOHO_TOKEN_URI             = "https://accounts.zoho.eu/oauth/v2/token"
    ZOHO_API_URI               = "https://www.zohoapis.eu/crm/v6"
    EXACTONLINE_CLIENT_ID      = "5c6b0dc4-2e78-4116-89c2-79e6e73356d8"
    EXACTONLINE_CLIENT_SECRET  = "XMSrmWMZkABv"
    EXACTONLINE_WEBHOOK_SECRET = "8vXq0eEHEhEc6iwn"
    EXACTONLINE_REDIRECT_URI   = "https://${var.domain}/exactonline/callback"
    EXACTONLINE_BASE_URL       = "https://start.exactonline.nl"
    EXACTONLINE_API_BASE       = "https://start.exactonline.nl/api/v1/2655637"
    EXACTONLINE_AUTHORIZE_PATH = "api/oauth2/auth"
    EXACTONLINE_TOKEN_PATH     = "api/oauth2/token"
    EXACTONLINE_BASE_URI       = "https://start.exactonline.nl"
    EXACTONLINE_DIVISION       = "2655637"
    EXACTONLINE_LEAD_SOURCE_ID = "945be231-9588-413e-a6cd-53c190669ea7"
    S3_ENDPOINT                = var.s3_server
    S3_ACCESS_KEY              = module.s3-tenant.access_key
    S3_SECRET_KEY              = module.s3-tenant.secret_key
    S3_BUCKET                  = module.s3-tenant.bucket
  }

  haproxy_services = [
    {
      name   = var.name
      domain = var.domain
      port   = "3000"
      host   = "127.0.0.1"
      tls    = true
    }
  ]
}

output "app_urls" {
  value = module.vw-hub.app_urls
}
