-- =========================================================================
-- MASTER: Senarai Rasmi 38 Anak Syarikat MBI (dikemas kini 6 Ogos 2026)
-- Jalankan di phpMyAdmin > tab SQL, pada database SPMS sedia ada anda.
--
-- SUMBER: disalin terus daripada senarai LIVE dalam admin/subsidiaries.php
-- (Kod, Nama, Sektor, Cluster, Status) — bukan anggapan/tafsiran, jadi
-- fail ini boleh dijadikan SATU sumber rujukan tunggal (canonical) untuk
-- 38 syarikat, menggantikan keperluan jejak balik merentasi
-- sql/import_32_subsidiaries_mbi.sql +
-- sql/migration_add_mbi_holding_and_excluded_companies.sql.
--
-- ⚠️ PENTING — JANGAN jalankan selepas migration_reconcile_subsidiary_
-- codes_jan2026.sql: fail ini guna kod LAMA (ISB, YANIS, INPEN_STDC)
-- sepadan dengan tangkapan skrin semasa anda. Kalau
-- migration_reconcile_subsidiary_codes_jan2026.sql SUDAH dijalankan
-- (menukar ISB->INVEST, YANIS->INSAN, INPEN_STDC->INPENS), menjalankan
-- fail INI selepas itu akan CIPTA SEMULA kod lama sebagai baris
-- DUPLIKAT berasingan (disahkan semasa ujian — 38 jadi 41 baris).
-- Jalankan HANYA SATU daripada dua fail ini, bukan kedua-duanya:
--   - Kod syarikat anda MASIH macam tangkapan skrin (ISB/YANIS/
--     INPEN_STDC) -> guna fail INI sahaja.
--   - Anda SUDAH jalankan migration_reconcile_subsidiary_codes_
--     jan2026.sql -> JANGAN jalankan fail ini, kod dah terkini.
--
-- SELAMAT dijalankan berulang kali (idempotent):
--   - Semakan information_schema sebelum ALTER TABLE (elak ralat
--     "#1060 Duplicate column").
--   - INSERT ... ON DUPLICATE KEY UPDATE ikut 'code' (UNIQUE KEY) — kalau
--     kod sudah wujud, kemaskini nama/sektor/cluster/status sahaja
--     (subsidiary_id/UUID SEDIA ADA dikekalkan, jadi data kewangan
--     sejarah yang dikaitkan dengan syarikat itu TIDAK terjejas). Kalau
--     kod belum wujud, rekod baharu dicipta.
--   - TIADA syarikat dipadam — kalau kod dalam sistem TIDAK disenaraikan
--     di sini, ia dibiarkan seperti sedia ada (skrip ini tidak pernah
--     memadam/menyahaktifkan apa-apa secara automatik).
-- =========================================================================

-- ---------- Sahkan lajur cluster/sector wujud (selamat jika sudah ada) ----------
SET @col_exists = (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'subsidiaries' AND COLUMN_NAME = 'cluster');
SET @ddl_sql = IF(@col_exists = 0, 'ALTER TABLE subsidiaries ADD COLUMN cluster ENUM(''commercial'',''strategy'',''holding'') NOT NULL DEFAULT ''commercial'' AFTER name', 'SELECT ''cluster sudah wujud'' AS info');
PREPARE s1 FROM @ddl_sql; EXECUTE s1; DEALLOCATE PREPARE s1;

-- Pastikan ENUM cluster merangkumi 'holding' (versi lama mungkin cuma commercial/strategy)
ALTER TABLE subsidiaries MODIFY COLUMN cluster ENUM('commercial','strategy','holding') NOT NULL DEFAULT 'commercial';

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 ''sector sudah wujud'' AS info');
PREPARE s2 FROM @ddl_sql; EXECUTE s2; DEALLOCATE PREPARE s2;

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

-- ---------- Semakan pantas selepas jalan ----------
SELECT COUNT(*) AS jumlah_syarikat FROM subsidiaries WHERE is_active = 1;
