<?php
declare(strict_types=1);

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

    $rules = [
        [
            'rule_code'          => 'ACQUIRED_CHILD_FEE_UNDER_12',
            'category'           => 'children_fee',
            'name_ar'            => 'رسوم ابن المنضم - أقل من 12 سنة',
            'name_en'            => 'Acquired Member Child Fee - Under 12',
            'data_type'          => 'percentage',
            'current_value_json' => '{"percentage":"15.00","base":"membership_value"}',
            'parameters_json'    => '{"percentage":"decimal","base":"string"}',
        ],
        [
            'rule_code'          => 'ACQUIRED_CHILD_FEE_12_TO_16',
            'category'           => 'children_fee',
            'name_ar'            => 'رسوم ابن المنضم - 12 إلى 16 سنة',
            'name_en'            => 'Acquired Member Child Fee - 12 to 16',
            'data_type'          => 'percentage',
            'current_value_json' => '{"percentage":"20.00","base":"membership_value"}',
            'parameters_json'    => '{"percentage":"decimal","base":"string"}',
        ],
        [
            'rule_code'          => 'ACQUIRED_CHILD_FEE_16_TO_18',
            'category'           => 'children_fee',
            'name_ar'            => 'رسوم ابن المنضم - 16 إلى 18 سنة',
            'name_en'            => 'Acquired Member Child Fee - 16 to 18',
            'data_type'          => 'percentage',
            'current_value_json' => '{"percentage":"25.00","base":"membership_value"}',
            'parameters_json'    => '{"percentage":"decimal","base":"string"}',
        ],
        [
            'rule_code'          => 'ACQUIRED_CHILD_FEE_OVER_18',
            'category'           => 'children_fee',
            'name_ar'            => 'رسوم ابن المنضم - أكبر من 18 سنة',
            'name_en'            => 'Acquired Member Child Fee - Over 18',
            'data_type'          => 'percentage',
            'current_value_json' => '{"percentage":"30.00","base":"membership_value"}',
            'parameters_json'    => '{"percentage":"decimal","base":"string"}',
        ],
        [
            'rule_code'          => 'ACQUIRED_SPOUSE_FEE_2ND_PLUS',
            'category'           => 'spouse_fee',
            'name_ar'            => 'رسوم زوجة المنضم - الثانية فأكثر',
            'name_en'            => 'Acquired Member Spouse Fee - 2nd+',
            'data_type'          => 'percentage',
            'current_value_json' => '{"percentage":"75.00","base":"membership_value"}',
            'parameters_json'    => '{"percentage":"decimal","base":"string"}',
        ],
    ];

    foreach ($rules as $rule) {
        $exists = $db->selectOne(
            "SELECT id FROM business_rules WHERE rule_code = ? AND branch_id IS NULL",
            [$rule['rule_code']]
        );
        if ($exists) {
            continue;
        }

        $db->insert('business_rules', array_merge($rule, [
            'effective_from' => '2024-01-01',
            'is_active'      => 1,
            'version'        => 1,
            'created_at'     => $now,
            'updated_at'     => $now,
        ]));
    }
};
