-- pgTAP: public.users.theme column (migration 015)
-- Run with: psql -U postgres -d postgres -f supabase/tests/009_user_theme.sql
--
-- Fase 9.1 — dark mode feature flagship. Adds `theme` to public.users with
-- three valid values: 'light' | 'dark' | 'system'. New users default to
-- 'system' so the UI matches the OS preference until they opt in.

CREATE EXTENSION IF NOT EXISTS pgtap;

BEGIN;
SELECT plan(8);

-- ── Column existence + type ─────────────────────────────────────────────
SELECT has_column(
    'public', 'users', 'theme',
    'public.users has a theme column'
);

SELECT col_type_is(
    'public', 'users', 'theme', 'text',
    'public.users.theme is of type text'
);

SELECT col_not_null(
    'public', 'users', 'theme',
    'public.users.theme is NOT NULL'
);

SELECT col_default_is(
    'public', 'users', 'theme', 'system',
    'public.users.theme defaults to system'
);

-- ── Check constraint behaviour ──────────────────────────────────────────
-- Use Ana (seed admin) so the row exists.
UPDATE public.users SET theme = 'light'
WHERE id = '11111111-1111-1111-1111-111111111111';
SELECT is(
    (SELECT theme FROM public.users WHERE id = '11111111-1111-1111-1111-111111111111'),
    'light',
    'theme can be set to light'
);

UPDATE public.users SET theme = 'dark'
WHERE id = '11111111-1111-1111-1111-111111111111';
SELECT is(
    (SELECT theme FROM public.users WHERE id = '11111111-1111-1111-1111-111111111111'),
    'dark',
    'theme can be set to dark'
);

UPDATE public.users SET theme = 'system'
WHERE id = '11111111-1111-1111-1111-111111111111';
SELECT is(
    (SELECT theme FROM public.users WHERE id = '11111111-1111-1111-1111-111111111111'),
    'system',
    'theme can be set to system'
);

-- Invalid value must be rejected by the check constraint.
SELECT throws_ok(
    $$UPDATE public.users SET theme = 'sepia' WHERE id = '11111111-1111-1111-1111-111111111111'$$,
    '23514',
    NULL,
    'theme rejects values other than light|dark|system'
);

SELECT * FROM finish();
ROLLBACK;
