<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $ts = date('Y-m-d H:i:s');
    $now = date('Y-m-d');

    $services = [
        ['code' => 'SVC_NEW_FORM',        'name_ar' => 'استمارة عضوية جديدة',    'name_en' => 'New Membership Form',      'price_type' => 'fixed',      'base_amount' => '505.00'],
        ['code' => 'SVC_TRANSFER_FORM',    'name_ar' => 'استمارة تحويل / فصل',    'name_en' => 'Transfer/Separation Form', 'price_type' => 'fixed',      'base_amount' => '570.00'],
        ['code' => 'SVC_ADDITION_FORM',    'name_ar' => 'استمارة إضافة',          'name_en' => 'Addition Form',            'price_type' => 'fixed',      'base_amount' => '570.00'],
        ['code' => 'SVC_DEV_FEE',          'name_ar' => 'رسوم تنمية',             'name_en' => 'Development Fee',          'price_type' => 'fixed',      'base_amount' => '35.00'],
        ['code' => 'SVC_MARTYRS_STAMP',    'name_ar' => 'طابع شهداء',             'name_en' => 'Martyrs Stamp',            'price_type' => 'fixed',      'base_amount' => '5.00'],
        ['code' => 'SVC_CARNET_REPLACE',   'name_ar' => 'بدل فاقد كارنيه',        'name_en' => 'Carnet Replacement',       'price_type' => 'fixed',      'base_amount' => '200.00'],
        ['code' => 'SVC_SEASONAL',         'name_ar' => 'عضوية موسمية',           'name_en' => 'Seasonal Membership',      'price_type' => 'percentage', 'percentage' => '5.00'],
        ['code' => 'SVC_TEMP_MEMBER',      'name_ar' => 'رسوم عضو مؤقت',          'name_en' => 'Temporary Member Fee',     'price_type' => 'percentage', 'percentage' => '10.00'],
        ['code' => 'SVC_CHILD_4TH',        'name_ar' => 'رسوم ابن رابع',          'name_en' => '4th Child Fee',            'price_type' => 'percentage', 'percentage' => '5.00'],
        ['code' => 'SVC_CHILD_18',         'name_ar' => 'رسوم ابن 18 سنة',        'name_en' => 'Child 18 Fee',             'price_type' => 'percentage', 'percentage' => '10.00'],
        ['code' => 'SVC_CHILD_19',         'name_ar' => 'رسوم ابن 19 سنة',        'name_en' => 'Child 19 Fee',             'price_type' => 'percentage', 'percentage' => '15.00'],
        ['code' => 'SVC_CHILD_20',         'name_ar' => 'رسوم ابن 20 سنة',        'name_en' => 'Child 20 Fee',             'price_type' => 'percentage', 'percentage' => '20.00'],
        ['code' => 'SVC_CHILD_21',         'name_ar' => 'رسوم ابن 21 سنة',        'name_en' => 'Child 21 Fee',             'price_type' => 'percentage', 'percentage' => '15.00'],
        ['code' => 'SVC_SPOUSE_2ND',       'name_ar' => 'رسوم زوجة ثانية',        'name_en' => '2nd Spouse Fee',           'price_type' => 'percentage_plus_annual', 'percentage' => '10.00', 'annual_amount' => '150.00'],
        ['code' => 'SVC_SPOUSE_3RD',       'name_ar' => 'رسوم زوجة ثالثة',        'name_en' => '3rd Spouse Fee',           'price_type' => 'percentage_plus_annual', 'percentage' => '20.00', 'annual_amount' => '200.00'],
        ['code' => 'SVC_SPOUSE_4TH',       'name_ar' => 'رسوم زوجة رابعة',        'name_en' => '4th Spouse Fee',           'price_type' => 'percentage_plus_annual', 'percentage' => '30.00', 'annual_amount' => '300.00'],
        ['code' => 'SVC_SPOUSE_FOREIGN',   'name_ar' => 'رسوم زوج أجنبي',         'name_en' => 'Foreign Spouse Fee',       'price_type' => 'percentage', 'percentage' => '15.00'],
        ['code' => 'SVC_SPOUSE_ACQUIRED',  'name_ar' => 'رسوم زوج مكتسب',         'name_en' => 'Acquired Spouse Fee',      'price_type' => 'percentage', 'percentage' => '50.00'],
        ['code' => 'SVC_SPOUSE_BASE',      'name_ar' => 'رسوم زوج أساسي',         'name_en' => 'Base Spouse Fee',          'price_type' => 'percentage', 'percentage' => '15.00'],
        ['code' => 'SVC_WAIVER',           'name_ar' => 'رسوم تنازل',             'name_en' => 'Waiver Fee',              'price_type' => 'percentage', 'percentage' => '30.00'],
        // SVC_SPORTS_CONV removed — no implementation exists, pricing lives in business_rules
        ['code' => 'SVC_ANNUAL_MEMBER',    'name_ar' => 'اشتراك سنوي - عضو',      'name_en' => 'Annual Sub - Member',     'price_type' => 'fixed',      'base_amount' => '492.00'],
        ['code' => 'SVC_ANNUAL_SPOUSE',    'name_ar' => 'اشتراك سنوي - زوجة',     'name_en' => 'Annual Sub - Spouse',     'price_type' => 'fixed',      'base_amount' => '492.00'],
        ['code' => 'SVC_ANNUAL_CHILD',     'name_ar' => 'اشتراك سنوي - ابن/ابنة', 'name_en' => 'Annual Sub - Child',      'price_type' => 'fixed',      'base_amount' => '222.00'],
        ['code' => 'SVC_ANNUAL_TEMP',      'name_ar' => 'اشتراك سنوي - مؤقت',     'name_en' => 'Annual Sub - Temp',       'price_type' => 'fixed',      'base_amount' => '222.00'],
    ];

    foreach ($services as $s) {
        $existing = $db->selectOne("SELECT id FROM service_catalog WHERE service_code = ? AND branch_id IS NULL", [$s['code']]);
        if ($existing) {
            continue;
        }
        $db->insert('service_catalog', [
            'service_code'   => $s['code'],
            'name_ar'        => $s['name_ar'],
            'name_en'        => $s['name_en'] ?? null,
            'price_type'     => $s['price_type'],
            'base_amount'    => $s['base_amount'] ?? null,
            'percentage'     => $s['percentage'] ?? null,
            'annual_amount'  => $s['annual_amount'] ?? null,
            'currency'       => 'EGP',
            'applies_to'     => $s['applies_to'] ?? null,
            'branch_id'      => null,
            'effective_from' => $now,
            'is_active'      => 1,
            'created_at'     => $ts,
            'updated_at'     => $ts,
        ]);
    }
};