-- Settings page (t-paliad-022): per-user email notification preferences and
-- a non-null language column.
--
-- email_preferences is a free-form JSONB bag. The app reads well-known keys
-- today (deadline_reminders, deadline_reminders.overdue, .tomorrow, .weekly);
-- unknown keys are ignored. An empty object means "all reminders on" — the
-- reminder_service treats a missing key as opt-in so existing users keep the
-- behaviour they had before the settings page shipped.
--
-- lang was added nullable in 016. Now that the settings page lets every user
-- pick DE/EN, backfill any remaining NULLs to the German default and enforce
-- NOT NULL so downstream code (reminder templates) can stop dancing around
-- a *string.

ALTER TABLE paliad.users
    ADD COLUMN IF NOT EXISTS email_preferences jsonb NOT NULL DEFAULT '{}'::jsonb;

UPDATE paliad.users SET lang = 'de' WHERE lang IS NULL;

ALTER TABLE paliad.users
    ALTER COLUMN lang SET NOT NULL,
    ALTER COLUMN lang SET DEFAULT 'de';
