-- 20260327_005_hybrid_license_hwid.sql

ALTER TABLE customers
    MODIFY COLUMN plan_type VARCHAR(20) NOT NULL DEFAULT 'L-SYNC',
    ADD COLUMN IF NOT EXISTS desktop_expiry_date DATETIME NULL AFTER expiry_date,
    ADD COLUMN IF NOT EXISTS sync_expiry_date DATETIME NULL AFTER desktop_expiry_date;

UPDATE customers
SET plan_type = 'L-SYNC'
WHERE TRIM(COALESCE(plan_type, '')) IN ('1', '2', '3');

UPDATE customers
SET max_pc_devices = GREATEST(1, COALESCE(max_pc_devices, max_devices, 1));

UPDATE customers
SET max_web_sessions = GREATEST(0, COALESCE(max_web_sessions, 0));

UPDATE customers
SET max_devices = max_pc_devices
WHERE COALESCE(max_devices, 0) <> max_pc_devices;

UPDATE customers
SET max_pc_devices = 1
WHERE plan_type IN ('L-SOLO', 'L-SYNC', 'A-SOLO');

UPDATE customers
SET max_web_sessions = 0
WHERE plan_type IN ('L-SOLO', 'A-SOLO', 'A-OFFICE');

UPDATE customers
SET sync_expiry_date = expiry_date
WHERE plan_type = 'L-SYNC'
  AND (sync_expiry_date IS NULL OR sync_expiry_date = '')
  AND expiry_date IS NOT NULL
  AND expiry_date <> '';

UPDATE customers
SET desktop_expiry_date = expiry_date
WHERE plan_type IN ('A-SOLO', 'A-OFFICE', 'A-COMPLEX')
  AND (desktop_expiry_date IS NULL OR desktop_expiry_date = '')
  AND expiry_date IS NOT NULL
  AND expiry_date <> '';

UPDATE customers
SET sync_expiry_date = expiry_date
WHERE plan_type IN ('A-OFFICE', 'A-COMPLEX')
  AND (sync_expiry_date IS NULL OR sync_expiry_date = '')
  AND expiry_date IS NOT NULL
  AND expiry_date <> '';

UPDATE customers
SET desktop_expiry_date = NULL
WHERE plan_type IN ('L-SOLO', 'L-SYNC');

UPDATE customers
SET sync_expiry_date = NULL
WHERE plan_type IN ('L-SOLO', 'A-SOLO');

UPDATE customers
SET expiry_date = desktop_expiry_date
WHERE desktop_expiry_date IS NOT NULL;

UPDATE customers
SET expiry_date = NULL
WHERE plan_type IN ('L-SOLO', 'L-SYNC');

DELETE FROM plan_feature_map
WHERE plan_type IN ('1', '2', '3');

DELETE FROM plan_catalog
WHERE plan_type IN ('1', '2', '3');

INSERT INTO plan_catalog (plan_type, plan_name, pc_limit_mode, web_limit_mode)
VALUES
    ('L-SOLO', 'L-Solo (Omur Boyu Tekli)', 'fixed_1', 'fixed_0'),
    ('L-SYNC', 'L-Sync (Omur Boyu + Telefon)', 'fixed_1', 'editable'),
    ('A-SOLO', 'A-Solo (Yillik Lokal)', 'fixed_1', 'fixed_0'),
    ('A-OFFICE', 'A-Office (Yillik Coklu PC)', 'editable', 'fixed_0'),
    ('A-COMPLEX', 'A-Complex (Yillik Tam Paket)', 'editable', 'editable')
ON DUPLICATE KEY UPDATE
    plan_name = VALUES(plan_name),
    pc_limit_mode = VALUES(pc_limit_mode),
    web_limit_mode = VALUES(web_limit_mode);

INSERT INTO plan_feature_defaults (feature_key, description_text)
VALUES
    ('WEB_ACCESS', 'Web/mobile panel access'),
    ('BACKUP_AUTO', 'Automatic backup'),
    ('BACKUP_MANUAL', 'Manual backup'),
    ('DEVICE_MANAGEMENT', 'Device management panel'),
    ('PROFILE_SYNC', 'Desktop profile sync'),
    ('PAYMENT_REQUEST', 'Payment request flow'),
    ('DEMO_CREATE', 'Demo creation')
ON DUPLICATE KEY UPDATE
    description_text = VALUES(description_text);

INSERT INTO plan_feature_map (plan_type, feature_key, is_enabled)
VALUES
    ('L-SOLO', 'WEB_ACCESS', 0),
    ('L-SOLO', 'BACKUP_AUTO', 0),
    ('L-SOLO', 'BACKUP_MANUAL', 0),
    ('L-SOLO', 'DEVICE_MANAGEMENT', 1),
    ('L-SOLO', 'PROFILE_SYNC', 0),
    ('L-SOLO', 'PAYMENT_REQUEST', 1),
    ('L-SOLO', 'DEMO_CREATE', 1),
    ('L-SYNC', 'WEB_ACCESS', 1),
    ('L-SYNC', 'BACKUP_AUTO', 1),
    ('L-SYNC', 'BACKUP_MANUAL', 1),
    ('L-SYNC', 'DEVICE_MANAGEMENT', 1),
    ('L-SYNC', 'PROFILE_SYNC', 1),
    ('L-SYNC', 'PAYMENT_REQUEST', 1),
    ('L-SYNC', 'DEMO_CREATE', 1),
    ('A-SOLO', 'WEB_ACCESS', 0),
    ('A-SOLO', 'BACKUP_AUTO', 0),
    ('A-SOLO', 'BACKUP_MANUAL', 0),
    ('A-SOLO', 'DEVICE_MANAGEMENT', 1),
    ('A-SOLO', 'PROFILE_SYNC', 0),
    ('A-SOLO', 'PAYMENT_REQUEST', 1),
    ('A-SOLO', 'DEMO_CREATE', 1),
    ('A-OFFICE', 'WEB_ACCESS', 0),
    ('A-OFFICE', 'BACKUP_AUTO', 0),
    ('A-OFFICE', 'BACKUP_MANUAL', 0),
    ('A-OFFICE', 'DEVICE_MANAGEMENT', 1),
    ('A-OFFICE', 'PROFILE_SYNC', 1),
    ('A-OFFICE', 'PAYMENT_REQUEST', 1),
    ('A-OFFICE', 'DEMO_CREATE', 1),
    ('A-COMPLEX', 'WEB_ACCESS', 1),
    ('A-COMPLEX', 'BACKUP_AUTO', 1),
    ('A-COMPLEX', 'BACKUP_MANUAL', 1),
    ('A-COMPLEX', 'DEVICE_MANAGEMENT', 1),
    ('A-COMPLEX', 'PROFILE_SYNC', 1),
    ('A-COMPLEX', 'PAYMENT_REQUEST', 1),
    ('A-COMPLEX', 'DEMO_CREATE', 1)
ON DUPLICATE KEY UPDATE
    is_enabled = VALUES(is_enabled);
