-- Migration 015: theme preference on public.users (Fase 9.1)
--
-- Stores the user's chosen UI theme: 'light' | 'dark' | 'system'.
-- New users (and existing rows on upgrade) default to 'system' so the UI
-- inherits the OS preference until they explicitly opt in.
--
-- The JWT minted by GoTrue caches `auth.users` (not `public.users`), so
-- adding this column does NOT invalidate existing sessions — clients read
-- the value via PostgREST `SELECT theme FROM users WHERE id = auth.uid()`.

ALTER TABLE public.users
    ADD COLUMN theme text NOT NULL DEFAULT 'system'
    CHECK (theme IN ('light', 'dark', 'system'));

COMMENT ON COLUMN public.users.theme IS
    'UI theme preference. ''system'' follows prefers-color-scheme.';
