-- Migration 018: User preferences (Fase 4 / G12)
-- Data: 2026-05-29
--
-- Aggiunge a `users` colonne per preferenze:
--   - theme       (light|dark|auto)
--   - timezone    (default Europe/Rome — vedi CLAUDE.md sez. timezone)
--   - notif_email (notifiche via mail on/off)
--   - notif_inapp (notifiche in-app on/off)
--
-- Rollback:
--   ALTER TABLE users
--     DROP COLUMN notif_inapp, DROP COLUMN notif_email,
--     DROP COLUMN timezone, DROP COLUMN theme;

SET @c1 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='users' AND COLUMN_NAME='theme');
SET @s1 := IF(@c1 = 0,
  'ALTER TABLE users ADD COLUMN theme ENUM(''light'',''dark'',''auto'') DEFAULT ''auto'' AFTER preferred_language',
  'SELECT ''theme già presente — skip'' AS info'
);
PREPARE stmt FROM @s1; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c2 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='users' AND COLUMN_NAME='timezone');
SET @s2 := IF(@c2 = 0,
  'ALTER TABLE users ADD COLUMN timezone VARCHAR(64) DEFAULT ''Europe/Rome'' AFTER theme',
  'SELECT ''timezone già presente — skip'' AS info'
);
PREPARE stmt FROM @s2; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c3 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='users' AND COLUMN_NAME='notif_email');
SET @s3 := IF(@c3 = 0,
  'ALTER TABLE users ADD COLUMN notif_email TINYINT(1) DEFAULT 1 AFTER timezone',
  'SELECT ''notif_email già presente — skip'' AS info'
);
PREPARE stmt FROM @s3; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c4 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='users' AND COLUMN_NAME='notif_inapp');
SET @s4 := IF(@c4 = 0,
  'ALTER TABLE users ADD COLUMN notif_inapp TINYINT(1) DEFAULT 1 AFTER notif_email',
  'SELECT ''notif_inapp già presente — skip'' AS info'
);
PREPARE stmt FROM @s4; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT, COLUMN_COMMENT
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='users'
  AND COLUMN_NAME IN ('theme','timezone','notif_email','notif_inapp');
