-- =========================================================================
-- MIGRATION + IMPORT: Senarai Rasmi 32 Anak Syarikat MBI (29 Julai 2026)
-- Jalankan di phpMyAdmin > tab SQL, pada database SPMS sedia ada anda.
-- =========================================================================

-- ---------- 1) Tambah medan Sector ----------
-- Guna semakan information_schema (BUKAN "ADD COLUMN IF NOT EXISTS")
-- supaya serasi dengan MySQL 5.6/5.7 lama yang masih biasa pada hosting
-- cPanel perkongsian (sintaks IF NOT EXISTS untuk ADD COLUMN hanya
-- disokong MySQL 8.0.29+ / MariaDB 10.0.2+). Kalau lajur 'sector' SUDAH
-- wujud (cth. skrip ini pernah dijalankan sebelum ini, atau ditambah
-- melalui migrasi lain), langkau ALTER TABLE — elak ralat
-- "#1060 - Duplicate column name 'sector'".
SET @col_exists = (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'subsidiaries' AND COLUMN_NAME = 'sector'
);
SET @ddl_sql = IF(@col_exists = 0,
    'ALTER TABLE subsidiaries ADD COLUMN sector VARCHAR(100) NULL AFTER cluster',
    'SELECT ''Lajur sector sudah wujud — langkau ALTER TABLE.'' AS info'
);
PREPARE stmt_add_sector FROM @ddl_sql;
EXECUTE stmt_add_sector;
DEALLOCATE PREPARE stmt_add_sector;

-- ---------- 2) (PILIHAN) Bersihkan data demo sebelum ini ----------
-- Kod SSDU, SMARTSEL, dan KPS yang digunakan untuk data DEMO bertindih
-- dengan kod SEBENAR dalam senarai rasmi di bawah. Jalankan baris ini
-- untuk buang SEMUA data demo (5 syarikat + Financial/HR/KPI/Business
-- Plan/Notes berkaitan — cascade automatik) SEBELUM import data sebenar.
-- LANGKAU bahagian ini kalau anda nak kekalkan data demo dan import
-- data sebenar guna kod berlainan sahaja.
--
-- DELETE FROM subsidiaries WHERE code IN ('SSDU','SMARTSEL','YHIJRAH','KPS','AIRSEL');

-- ---------- 3) Import 32 Anak Syarikat Rasmi ----------
-- Guna INSERT ... ON DUPLICATE KEY UPDATE supaya selamat dijalankan
-- berulang kali (kemaskini nama/cluster/sector kalau kod sudah wujud,
-- bukan cipta duplikat).

INSERT INTO subsidiaries (subsidiary_id, code, name, cluster, sector, is_active) VALUES
(UUID(), 'SSDU',        'SSDU Innovations Sdn Bhd',                          'strategy',   'Media & Digital AI', 1),
(UUID(), 'MSSB',        'Media Selangor Sdn Bhd',                            'strategy',   'Media & Digital AI', 1),
(UUID(), 'UNISEL',      'UNISEL Sdn Bhd',                                    'commercial', 'Healthcare & Education', 1),
(UUID(), 'INPEN_STDC',  'INPEN (Selangor) Sdn Bhd',                          'commercial', 'Healthcare & Education', 1),
(UUID(), 'ISB',         'Invest Selangor Berhad',                            'strategy',   'Invest, Tourism & Social Promotion', 1),
(UUID(), 'TSSB',        'Tourism Selangor Sdn Bhd',                          'strategy',   'Invest, Tourism & Social Promotion', 1),
(UUID(), 'HIS',         'HIS Toyyiba Sdn Bhd',                               'strategy',   'Invest, Tourism & Social Promotion', 1),
(UUID(), 'YAWAS',       'Yayasan Warisan Anak Selangor',                     'strategy',   'Invest, Tourism & Social Promotion', 1),
(UUID(), 'WBS',         'Wanita Berdaya Selangor Sdn Bhd',                   'strategy',   'Invest, Tourism & Social Promotion', 1),
(UUID(), 'YANIS',       'Yayasan Insan Istimewa Selangor',                   'strategy',   'Invest, Tourism & Social Promotion', 1),
(UUID(), 'KHSB',        'Kumpulan Hartanah Selangor Berhad',                 'commercial', 'Land & Property', 1),
(UUID(), 'CSSB',        'Central Spectrum (M) Sdn Bhd',                      'commercial', 'Land & Property', 1),
(UUID(), 'PNSB',        'Permodalan Negeri Selangor Berhad',                 'commercial', 'Land & Property', 1),
(UUID(), 'LLSB',        'Landasan Lumayan Sdn Bhd',                          'commercial', 'Land & Property', 1),
(UUID(), 'BBGC',        'Bukit Beruntung Golf & Country Resort Berhad',      'commercial', 'Land & Property', 1),
(UUID(), 'PASB',        'Pengurusan Air Selangor Sdn Bhd',                   'commercial', 'Water', 1),
(UUID(), 'AISHOLDINGS', 'Air Selangor Holdings Berhad',                      'commercial', 'Water', 1),
(UUID(), 'HELIOSEL',    'Heliosel Sdn Bhd',                                  'commercial', 'Water', 1),
(UUID(), 'CUBIQMETERS', 'Cubiq Meters Sdn Bhd',                              'commercial', 'Water', 1),
(UUID(), 'AIRLESTARI',  'Air Lestari Sdn Bhd',                               'commercial', 'Water', 1),
(UUID(), 'HIJRAH',      'Yayasan Hijrah Selangor',                           'commercial', 'Retail & Financial Services', 1),
(UUID(), 'KSSB',        'Kumpulan Semesta Sdn Bhd',                          'commercial', 'Mining & Minerals', 1),
(UUID(), 'SIDEC',       'SIDEC Sdn Bhd',                                     'commercial', 'Start Up & VC', 1),
(UUID(), 'WAVPAY',      'Wavpay Fintech Holdings Sdn Bhd',                   'commercial', 'Retail & Financial Services', 1),
(UUID(), 'RMSB',        'Rantaian Mesra Sdn Bhd',                            'commercial', 'Retail & Financial Services', 1),
(UUID(), 'KPS',         'Kumpulan Perangsang Selangor Berhad',               'commercial', 'Investment', 1),
(UUID(), 'SELATI',      'Selangor Aviation & Technology Innovation Sdn Bhd', 'commercial', 'Investment', 1),
(UUID(), 'KDEBWM',      'KDEB Waste Management Sdn Bhd',                     'commercial', 'Energy & Utilities', 1),
(UUID(), 'KUSEL',       'Koridor Utiliti Selangor Sdn Bhd',                  'commercial', 'Energy & Utilities', 1),
(UUID(), 'SMARTSEL',    'Smartsel Sdn Bhd',                                  'commercial', 'Energy & Utilities', 1),
(UUID(), 'DEFM',        'Darul Ehsan Facilities Management Sdn Bhd',         'commercial', 'Energy & Utilities', 1),
(UUID(), 'URBANCUBES',  'Urbancubes Sdn Bhd',                                'commercial', 'Energy & Utilities', 1)
ON DUPLICATE KEY UPDATE
    name = VALUES(name),
    cluster = VALUES(cluster),
    sector = VALUES(sector);
