const initialRemovals = [
    [/(\n)-- CBC - Complete Blood Count\n\(1,1,'Complete Blood Count \(CBC\)'[\s\S]*?\),\n/m, '$1'],
    [/(\n)-- LFTs - Liver Function Tests \(full sub-parameters\)\n\(4,2,'LFTs \(Liver Function Test\)'[\s\S]*?\),\n/m, '$1'],
    [/(\n)-- Lipid Profile \(full sub-parameters\)\n\(5,2,'Lipid Profile'[\s\S]*?\),\n/m, '$1'],
    [/(\n)-- UCE - Urine Complete Examination \(full sub-parameters\)\n\(8,5,'Urine Complete Examination'[\s\S]*?\),\n/m, '$1'],
    [/(\n)-- RFTs\n\(10,8,'RFTs \( Renal Function Tests\)'[\s\S]*?\),\n/m, '$1'],
    [/(\n)-- TSH, Prolactin\n\(28,6,'TSH \(Thyroid Stimulating Hormone\)'[\s\S]*?\),\n/m, '$1'],
];
initialRemovals.forEach(([pattern, repl], i) => {
    if (pattern.test(text)) {
        text = text.replace(pattern, repl);
        console.log('Removed initial insert', i + 1);
    }
});

text = text.replace(
    /\n-- ============================================================\n-- TSH \(Standalone\)[\s\S]*?WHERE `name` = 'TSH' AND \(`short_name` = 'TSH' OR `short_name` IS NULL\);\n/,
    '\n-- TSH (Standalone) excluded from installer — add manually via Admin Tests.\n'
);

text = text.replace(
    /\n-- ============================================================\n-- 6\. HEMATOLOGY — Complete Blood Count \(CBC\)[\s\S]*?WHERE `name` IN \('CBC','Complete Blood Count \(CBC\)'\) AND `short_name` IN \('CBC'\);\n/,
    '\n-- 6. CBC excluded from installer — add manually via Admin Tests.\n'
);

text = text.replace(
    /\n-- ============================================================\n-- 34\. URINE — Complete Urinalysis \/ UCE[\s\S]*?WHERE `name` = 'Urine Complete Examination';\n/,
    '\n-- 34. Urine Complete Examination excluded from installer — add manually via Admin Tests.\n'
);

text = text.replace(
    /\nINSERT INTO `tests` \(`category_id`,`name`,`short_name`[\s\S]*?WHERE NOT EXISTS \(SELECT 1 FROM `tests` WHERE `name`='TSH \(Thyroid Stimulating Hormone\)'\);\n/,
    '\n-- TSH insert excluded from installer.\n'
);

text = text.replace(
    /\n-- ============================================================\n-- TSH Level - Detailed with interpretation[\s\S]*?WHERE NOT EXISTS \(SELECT 1 FROM `tests` WHERE `name`='TSH Level \(Thyroid Stimulating Hormone\)'\);\n/,
    '\n-- TSH Level excluded from installer.\n'
);

text = text.replace(
    /\n-- Graphical Fasting Lipid Profile from the supplied OneLABz report\.[\s\S]*?WHERE `name` = 'Lipid Profile';\n/,
    '\n-- Lipid Profile patch excluded from installer.\n'
);

text = text.replace(
    /\nUPDATE `tests` SET `report_pattern` = 'graphical_range'[\s\S]*?WHERE `name` IN \('Lipid Profile'[\s\S]*?\);\n/,
    '\n'
);

text = text.replace(
    /\n-- Core endocrinology reference ranges\.\nUPDATE `tests` SET\n  `short_name`='TSH'[\s\S]*?WHERE LOWER\(`name`\) IN \('tsh','thyroid stimulating hormone','serum tsh'\);\n/,
    '\n-- Standalone TSH reference update excluded from installer.\n'
);

const cbcBlock = text.match(/SET @cbc_cat=[\s\S]*?DELETE FROM test_formulas WHERE test_id=@cbc_id;[\s\S]*?\n\n(INSERT INTO tests\n\(name,short_name,category_id)/);
if (cbcBlock) {
    text = text.replace(
        /SET @cbc_cat=[\s\S]*?DELETE FROM test_formulas WHERE test_id=@cbc_id;[\s\S]*?\n\n(INSERT INTO tests\n\(name,short_name,category_id)/,
        'SET @cbc_cat=(SELECT id FROM test_categories WHERE name=\'Hematology\' LIMIT 1);\nSET @cbc_dep=(SELECT id FROM laboratory_departments WHERE name=\'Hematology\' LIMIT 1);\nSET @tsh_cat=(SELECT id FROM test_categories WHERE name=\'Special Chemistry\' LIMIT 1);\nSET @tsh_dep=(SELECT id FROM laboratory_departments WHERE name=\'Special Chemistry\' LIMIT 1);\n\n-- CBC (Complete Blood Count) excluded from installer — add manually via Admin Tests.\n\n$1'
    );
    console.log('Stripped CBC from cbc_tsh block');
}

replaceBlock('-- Consolidated from 2026_07_26_lft_test_settings.sql', '-- LFT excluded from installer.\n');
replaceBlock('-- Consolidated from 2026_07_26_fasting_lipid_profile_settings.sql', '-- Lipid Profile excluded from installer.\n');
replaceBlock('-- Renal Function Test replacement from Rfts.png', '-- RFT excluded from installer.\n');
replaceBlock('-- Canonical Urine Complete Examination replacement from AdminTest screenshot', '-- Urine C/E excluded from installer.\n');
replaceBlock('-- Canonical standalone TSH replacement from tsh.png', '-- Standalone TSH excluded from installer.\n');

if (!text.includes('INSTALLER EXCLUSION')) {
    text = text.trimEnd() + EXCLUSION_BLOCK;
}