import type { PoolClient } from 'pg';

export type PreferredTheme = 'dark' | 'light';

export function normalizePreferredTheme(value: unknown): PreferredTheme {
  return value === 'light' ? 'light' : 'dark';
}

export async function ensureProfilePreferenceSchema(client: PoolClient): Promise<void> {
  await client.query(`
    ALTER TABLE profiles
      ADD COLUMN IF NOT EXISTS preferred_theme VARCHAR(16) NOT NULL DEFAULT 'dark'
  `);
  await client.query(`
    UPDATE profiles
       SET preferred_theme = 'dark'
     WHERE preferred_theme IS NULL
        OR preferred_theme NOT IN ('dark', 'light')
  `);
}
