# ---------------------------------------------------------------------------------
# Hosts
# ---------------------------------------------------------------------------------
# One entry per VPS. The same module is instantiated for each (see main.tf's
# for_each). This config manages TWO hosts: prod (new 48 GB / 12-core VPS, branch
# `release`, thermograph.org) and beta (the old VPS 75.119.132.91, branch `main`,
# no public domain by default). The `dev` branch deploys to the LAN dev server via
# deploy/deploy-dev.sh (a self-hosted runner, in the app repo) and is NOT managed here.
#
# A host with `domain = ""` gets no Caddy/TLS: the app port is opened on the firewall
# and the app is reached directly (beta does this by default; set a domain to front
# it with Caddy TLS).
#
# Resource sizing (workers / app_cpus / db_cpus / db_memory) defaults to the historical
# 4 / 4 / 2 / 8g budget (right for beta). The prod box is 48 GB / 12 cores — raise these
# there (the example uses app_cpus 8, db_cpus 4, db_memory "16g"). The Postgres internal
# budget scales from db_memory automatically (deploy/db/init/20-tuning.sh), so no
# separate tuning edit is needed to exploit the RAM.
variable "hosts" {
  description = "Map of hosts to manage, keyed by a short name (e.g. \"prod\", \"beta\")."
  type = map(object({
    host                 = string                     # IP or hostname to SSH to
    ssh_user             = optional(string, "deploy") # SSH login user
    ssh_private_key_path = string                     # path to the private key for that user
    role                 = string                     # "prod" | "beta" (informational + outputs)
    git_branch           = string                     # this INFRA repo's branch the checkout is reset to
    # Which app images to run, e.g. "sha-<12 hex>" (each matching build-push.yml's tag
    # for the commit that app repo built) or a semver tag on a release push. The app is
    # TWO separately-published images now — emi/thermograph-backend/app and
    # emi/thermograph-frontend/app — pinned independently. Required, no default: the
    # host's checkout is this infra repo, not the app repos, so there's no "current
    # commit" to derive a tag from; both must be explicit. Bump these (via a normal
    # tfvars edit + apply) whenever an app repo ships a commit you want this host
    # running; the infra repo's own git_branch is independent and rarely needs to change.
    backend_image_tag  = string
    frontend_image_tag = string
    domain        = optional(string, "") # public domain; "" => no Caddy/TLS
    compose_files = optional(list(string), ["docker-compose.yml"])
    app_dir       = optional(string, "/opt/thermograph") # checkout path on the host
    workers       = optional(number, 4)                  # uvicorn workers (WORKERS)
    app_cpus      = optional(number, 4)                  # app container CPU cap (APP_CPUS)
    db_cpus       = optional(number, 2)                  # db container CPU cap (DB_CPUS)
    db_memory     = optional(string, "8g")               # db container memory cap (DB_MEMORY)
    # A named size tier (see locals.sizes in main.tf: nano/small/medium/large) —
    # when set, overrides the four fields above with the tier's preset. Leave
    # null (default) to keep hand-picking workers/app_cpus/db_cpus/db_memory,
    # as prod/beta already do below.
    size = optional(string, null)
    # The floating tag matches today's behavior everywhere until you pin it. Pin to
    # an exact minor (SELECT extversion FROM pg_extension WHERE extname='timescaledb'
    # on the live DB) before any host of this stack could ever replicate with
    # another — a floating tag risks mismatched extension minors, which blocks a
    # physical replica (hop-1 cutover runbook hazard #7). Use the SAME tag everywhere.
    timescaledb_tag = optional(string, "latest-pg18")
    # Self-host the ERA5 archive (docker-compose.openmeteo.yml + a host rclone mount
    # of the object-storage bucket). Only the self-hosting host (prod) sets true.
    openmeteo   = optional(bool, false)
    om_data_dir = optional(string, "/mnt/om-archive") # host rclone mount point (OM_DATA_DIR)
  }))
}

# GCP-created hosts, keyed the same way as `hosts`. Default {} => zero GCP resources
# planned and the google provider is never actually invoked (see versions.tf and
# modules/gcp-host). Populate an entry to have Terraform create the VM itself; its
# output IP then feeds into the SAME thermograph-host module every SSH-managed host
# uses (main.tf), so provisioning logic is never duplicated between providers.
variable "gcp_hosts" {
  description = "Map of hosts for Terraform to CREATE on GCP (Compute Engine), keyed the same way as `hosts`. Empty by default -- no live GCP resources exist yet; this is a scaffold for future use. See modules/gcp-host."
  type = map(object({
    project              = string # GCP project ID
    zone                 = string # e.g. "us-west1-a"
    machine_type         = optional(string, "e2-medium")
    ssh_user             = optional(string, "deploy")
    ssh_public_key_path  = string # path to the PUBLIC key installed on the instance
    ssh_private_key_path = string # path to the matching PRIVATE key (for the module's provisioner)
    role                 = string
    git_branch           = string
    backend_image_tag    = string
    frontend_image_tag   = string
    domain               = optional(string, "")
    compose_files        = optional(list(string), ["docker-compose.yml"])
    app_dir              = optional(string, "/opt/thermograph")
    size                 = optional(string, "small")
    timescaledb_tag      = optional(string, "latest-pg18")
  }))
  default = {}
}

variable "repo_url" {
  description = "Git remote to clone from when a host has no checkout yet. Points at THIS repo (thermograph-infra) now, not the app repo -- the app's own source is never checked out on a host; only its published registry images are pulled (see var.hosts[*].backend_image_tag / frontend_image_tag). thermograph-infra is a private repo, so this typically needs embedded read credentials, e.g. a Forgejo deploy token: \"https://<token-name>:<token>@git.thermograph.org/emi/thermograph-infra.git\"."
  type        = string
  default     = "https://git.thermograph.org/emi/thermograph-infra.git"
  sensitive   = true
}

variable "app_port" {
  description = "Port the app binds inside the container / is health-checked on."
  type        = number
  default     = 8137
}

# ---------------------------------------------------------------------------------
# Self-hosted Open-Meteo (object storage) — consumed only by hosts with openmeteo=true
# ---------------------------------------------------------------------------------
# The ERA5 .om archive lives in an object-storage bucket, surfaced on the host by an
# rclone FUSE mount at each host's om_data_dir. These describe that bucket + mount.
# om_rclone_conf holds credentials, so it's sensitive and lands in state — keep the
# real value in terraform.tfvars (gitignored), never committed.
variable "om_bucket_remote" {
  description = "rclone remote:path for the archive bucket, e.g. \"om-archive:thermograph-era5\" (matches a [remote] in om_rclone_conf)."
  type        = string
  default     = ""
}

variable "om_rclone_conf" {
  description = "Full rclone.conf contents defining the archive remote (installed to /etc/rclone/rclone.conf, 0600). Sensitive."
  type        = string
  default     = ""
  sensitive   = true
}

variable "om_vfs_cache_max" {
  description = "rclone --vfs-cache-max-size: bounds the on-disk hot cache for the mount (keep within the disk budget)."
  type        = string
  default     = "80G"
}

# ---------------------------------------------------------------------------------
# Secrets: owned by the SOPS+age vault now, NOT Terraform
# ---------------------------------------------------------------------------------
# POSTGRES_PASSWORD, THERMOGRAPH_AUTH_SECRET, THERMOGRAPH_METRICS_TOKEN,
# THERMOGRAPH_INDEXNOW_KEY, THERMOGRAPH_VAPID_*, REGISTRY_TOKEN, Discord/SMTP
# credentials, and every other app secret are no longer Terraform variables --
# they're rendered at deploy time from deploy/secrets/*.yaml (SOPS-encrypted,
# committed) by deploy/render-secrets.sh, which deploy.sh calls before `docker
# compose up`. Terraform's job here shrank to topology/sizing only (see
# modules/thermograph-host/templates/thermograph-topology.env.tftpl) plus
# triggering the deploy. See deploy/secrets/README.md to rotate or add a secret.
#
# The four secrets Terraform used to generate itself (postgres_password,
# auth_secret, metrics_token, indexnow_key) still exist as values -- they just
# live in the vault now, seeded once from Terraform's own generated values via
# deploy/secrets/seed-from-live.sh so the handoff changed nothing in use. Rotate
# them the same way as any other vault secret from here on (sops edit + commit +
# deploy), not via a Terraform keeper change.
