-- =============================================================================
-- Per-user theme preference (Phase 9 — THEME-01, THEME-05)
-- =============================================================================
-- Adds a `theme` column to the Better Auth "user" table so light/dark/system
-- preference persists server-side and applies on sign-in across devices.
--
-- Allowed values: 'light' | 'dark' | 'system' (validated in API layer).
-- Default 'system' means next-themes detects OS preference at render time —
-- zero behavior change for existing signed-in users.
-- =============================================================================

ALTER TABLE "user"
  ADD COLUMN IF NOT EXISTS theme TEXT NOT NULL DEFAULT 'system';

-- Defensive backfill (DEFAULT covers new inserts, but on managed Postgres a
-- column added with DEFAULT may briefly show NULL in flight on some replicas).
UPDATE "user" SET theme = 'system' WHERE theme IS NULL;

COMMENT ON COLUMN "user".theme IS
  'User theme preference: light | dark | system. Applied app-wide via next-themes; server is canonical.';
