-- ============================================================
-- Gmind Institute — Fix Corrupted Offer Popup Settings
-- Run this SQL on the live database: advaysin_gmind
-- ============================================================
-- 
-- ROOT CAUSE: The settings table has DUAL unique columns:
--   key_name (UNIQUE) + value       — old schema
--   setting_key (UNIQUE) + setting_value — new schema
-- 
-- When the admin saved offer_popup settings, the INSERT only
-- filled setting_key/setting_value, leaving key_name as ''.
-- Since key_name has a UNIQUE constraint, every subsequent
-- INSERT collided on key_name='' and OVERWROTE the same row!
-- Result: only 1 corrupted row with offer_popup_enabled='Learn More'
-- ============================================================

-- 1. DELETE the corrupted offer_popup row(s)
DELETE FROM `settings` WHERE `setting_key` LIKE 'offer_popup_%' OR `key_name` LIKE 'offer_popup_%';

-- 2. Re-insert ALL 6 offer_popup settings correctly
-- Both key_name/value AND setting_key/setting_value are set
INSERT INTO `settings` (`key_name`, `value`, `setting_key`, `setting_value`) 
VALUES ('offer_popup_enabled', '0', 'offer_popup_enabled', '0')
ON DUPLICATE KEY UPDATE `setting_value` = '0', `value` = '0';

INSERT INTO `settings` (`key_name`, `value`, `setting_key`, `setting_value`) 
VALUES ('offer_popup_title', '', 'offer_popup_title', '')
ON DUPLICATE KEY UPDATE `setting_value` = '', `value` = '';

INSERT INTO `settings` (`key_name`, `value`, `setting_key`, `setting_value`) 
VALUES ('offer_popup_description', '', 'offer_popup_description', '')
ON DUPLICATE KEY UPDATE `setting_value` = '', `value` = '';

INSERT INTO `settings` (`key_name`, `value`, `setting_key`, `setting_value`) 
VALUES ('offer_popup_image', '', 'offer_popup_image', '')
ON DUPLICATE KEY UPDATE `setting_value` = '', `value` = '';

INSERT INTO `settings` (`key_name`, `value`, `setting_key`, `setting_value`) 
VALUES ('offer_popup_link', '', 'offer_popup_link', '')
ON DUPLICATE KEY UPDATE `setting_value` = '', `value` = '';

INSERT INTO `settings` (`key_name`, `value`, `setting_key`, `setting_value`) 
VALUES ('offer_popup_link_text', 'Learn More', 'offer_popup_link_text', 'Learn More')
ON DUPLICATE KEY UPDATE `setting_value` = 'Learn More', `value` = 'Learn More';

-- 3. Verify the fix
SELECT '--- Offer Popup Settings After Fix ---' AS info;
SELECT `setting_key`, `setting_value`, `key_name`, `value` FROM `settings` WHERE `setting_key` LIKE 'offer_popup_%';
