{"-- Add UI and TTS language preferences per user
-- 1) Add columns to user_preferences if not exist
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns 
    WHERE table_schema = 'public' AND table_name = 'user_preferences' AND column_name = 'ui_language'
  ) THEN
    ALTER TABLE public.user_preferences ADD COLUMN ui_language text;
  END IF;
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns 
    WHERE table_schema = 'public' AND table_name = 'user_preferences' AND column_name = 'tts_language'
  ) THEN
    ALTER TABLE public.user_preferences ADD COLUMN tts_language text;
  END IF;
END $$","-- 2) Backfill from existing language column when new columns are null
UPDATE public.user_preferences
SET ui_language = COALESCE(ui_language, language),
    tts_language = COALESCE(tts_language, language)
WHERE (ui_language IS NULL OR tts_language IS NULL)","-- 3) Optional: set simple constraints for known language codes via trigger-free approach (no strict check to avoid migration failures)
-- We avoid CHECK constraints to keep flexibility per guidelines."}