<?php
declare(strict_types=1);

/**
 * Phase 41: System Unification — Complete Service Catalog
 *
 * Adds missing service catalog entries for fees that were previously
 * hardcoded or only in business_rules. After this seed, all fees
 * are editable via /catalog admin with per-branch override support.
 */

return function (\App\Core\Database $db) {
    $now = date('Y-m-d H:i:s');
    $today = date('Y-m-d');

    $services = [
        [
            'service_code'  => 'SVC_CARNET_REPLACEMENT',
            'name_ar'       => 'رسوم بدل فاقد الكارنيه',
            'name_en'       => 'Carnet Replacement Fee',
            'price_type'    => 'fixed',
            'base_amount'   => '200.00',
            'percentage'    => null,
            'annual_amount' => null,
            'currency'      => 'EGP',
            'applies_to'    => 'member',
        ],
        [
            'service_code'  => 'SVC_SEASONAL_MEMBER',
            'name_ar'       => 'رسوم العضوية الموسمية',
            'name_en'       => 'Seasonal Membership Fee',
            'price_type'    => 'percentage',
            'base_amount'   => null,
            'percentage'    => '5.00',
            'annual_amount' => null,
            'currency'      => 'EGP',
            'applies_to'    => 'member',
        ],
        [
            'service_code'  => 'SVC_FOREIGN_MEMBER',
            'name_ar'       => 'رسوم عضوية الأجانب',
            'name_en'       => 'Foreign Member Fee',
            'price_type'    => 'fixed',
            'base_amount'   => '5000.00',
            'percentage'    => null,
            'annual_amount' => null,
            'currency'      => 'EGP',
            'applies_to'    => 'member',
        ],
        [
            'service_code'  => 'SVC_DIVORCE_FORM',
            'name_ar'       => 'استمارة انفصال / طلاق',
            'name_en'       => 'Divorce/Separation Form Fee',
            'price_type'    => 'fixed',
            'base_amount'   => '570.00',
            'percentage'    => null,
            'annual_amount' => null,
            'currency'      => 'EGP',
            'applies_to'    => 'member',
        ],
        [
            'service_code'  => 'SVC_DEATH_FORM',
            'name_ar'       => 'استمارة نقل عضوية (وفاة)',
            'name_en'       => 'Death Transfer Form Fee',
            'price_type'    => 'fixed',
            'base_amount'   => '570.00',
            'percentage'    => null,
            'annual_amount' => null,
            'currency'      => 'EGP',
            'applies_to'    => 'member',
        ],
        [
            'service_code'  => 'SVC_WAIVER_FORM',
            'name_ar'       => 'استمارة تنازل',
            'name_en'       => 'Waiver Form Fee',
            'price_type'    => 'fixed',
            'base_amount'   => '570.00',
            'percentage'    => null,
            'annual_amount' => null,
            'currency'      => 'EGP',
            'applies_to'    => 'member',
        ],
    ];

    foreach ($services as $svc) {
        $exists = $db->selectOne(
            "SELECT id FROM service_catalog WHERE service_code = ? AND branch_id IS NULL",
            [$svc['service_code']]
        );
        if ($exists) {
            continue;
        }
        $db->insert('service_catalog', array_merge($svc, [
            'branch_id'      => null,
            'effective_from' => $today,
            'effective_to'   => null,
            'is_active'      => 1,
            'created_at'     => $now,
            'updated_at'     => $now,
        ]));
    }
};
