/**
 * §4.2 INV8 version-table DDL contract — immutable records + atomic snapshot swap.
 *
 * Mutable columns (hosts.status/last_seen, sessions.last_attach_at) are NEVER updated in
 * place. A new version row is inserted into `host_versions` and the `hosts_current` pointer
 * is CAS-swapped, so a concurrent read sees whole-old or whole-new — never a torn read.
 *
 * These DDL strings are the FROZEN contract P3 must apply verbatim (the migration owner is
 * P3; relay-contracts owns the shape so every plan agrees on the version-chain semantics).
 */

/** Immutable per-host snapshot rows (append-only; `supersedes` forms an audit chain). */
export const HOST_VERSIONS_DDL = `CREATE TABLE IF NOT EXISTS host_versions (
  version_id  uuid        PRIMARY KEY,
  host_id     uuid        NOT NULL REFERENCES hosts(host_id),
  snapshot    jsonb       NOT NULL,
  created_at  timestamptz NOT NULL DEFAULT now(),
  supersedes  uuid        NULL REFERENCES host_versions(version_id)
);` as const

/** CAS-swapped pointer to the live snapshot (atomic current-version indirection). */
export const HOSTS_CURRENT_DDL = `CREATE TABLE IF NOT EXISTS hosts_current (
  host_id     uuid PRIMARY KEY REFERENCES hosts(host_id),
  version_id  uuid NOT NULL REFERENCES host_versions(version_id)
);` as const

/** Ordered DDL statements for the INV8 version tables. */
export const INV8_VERSION_TABLE_DDL = [HOST_VERSIONS_DDL, HOSTS_CURRENT_DDL] as const
