<?php
declare(strict_types=1);

use App\Core\Database;

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

    $ensureRule = function (array $rule) use ($db, $ts): void {
        $existing = $db->selectOne(
            "SELECT id FROM business_rules WHERE rule_code = ? AND branch_id IS NULL",
            [$rule['rule_code']]
        );
        if ($existing) {
            return;
        }
        $db->insert('business_rules', array_merge($rule, [
            'branch_id'      => null,
            'effective_from'  => date('Y-m-d'),
            'is_active'       => 1,
            'version'         => 1,
            'created_at'      => $ts,
            'updated_at'      => $ts,
        ]));
    };

    // ── Inventory & Sales Business Rules ────────────────────────
    $ensureRule([
        'rule_code'          => 'INV_LOW_STOCK_ALERT_DAYS',
        'category'           => 'inventory',
        'name_ar'            => 'عدد أيام تنبيه نقص المخزون',
        'name_en'            => 'Low Stock Alert Days',
        'data_type'          => 'number',
        'current_value_json' => '{"days":1}',
        'parameters_json'    => '{"days":"integer"}',
    ]);

    $ensureRule([
        'rule_code'          => 'INV_EXPIRY_ALERT_DAYS',
        'category'           => 'inventory',
        'name_ar'            => 'أيام التنبيه قبل انتهاء الصلاحية',
        'name_en'            => 'Expiry Alert Days',
        'data_type'          => 'number',
        'current_value_json' => '{"days":30}',
        'parameters_json'    => '{"days":"integer"}',
    ]);

    $ensureRule([
        'rule_code'          => 'INV_TRANSFER_REQUIRES_APPROVAL',
        'category'           => 'inventory',
        'name_ar'            => 'النقل بين المخازن يتطلب اعتماد',
        'name_en'            => 'Stock Transfer Requires Approval',
        'data_type'          => 'boolean',
        'current_value_json' => '{"value":true}',
        'parameters_json'    => '{"value":"boolean"}',
    ]);

    $ensureRule([
        'rule_code'          => 'INV_PO_REQUIRES_APPROVAL',
        'category'           => 'inventory',
        'name_ar'            => 'أوامر الشراء تتطلب اعتماد',
        'name_en'            => 'Purchase Orders Require Approval',
        'data_type'          => 'boolean',
        'current_value_json' => '{"value":true}',
        'parameters_json'    => '{"value":"boolean"}',
    ]);

    $ensureRule([
        'rule_code'          => 'INV_DEFAULT_TAX_RATE',
        'category'           => 'inventory',
        'name_ar'            => 'نسبة ضريبة القيمة المضافة',
        'name_en'            => 'Default VAT Rate',
        'data_type'          => 'number',
        'current_value_json' => '{"percentage":"14.00"}',
        'parameters_json'    => '{"percentage":"decimal"}',
    ]);

    $ensureRule([
        'rule_code'          => 'INV_DEPRECIATION_RUN_DAY',
        'category'           => 'inventory',
        'name_ar'            => 'يوم تشغيل الإهلاك الشهري',
        'name_en'            => 'Monthly Depreciation Run Day',
        'data_type'          => 'number',
        'current_value_json' => '{"day":1}',
        'parameters_json'    => '{"day":"integer"}',
    ]);

    $ensureRule([
        'rule_code'          => 'INV_MEMBER_DISCOUNT_PERCENTAGE',
        'category'           => 'inventory',
        'name_ar'            => 'نسبة خصم الأعضاء على المبيعات',
        'name_en'            => 'Member Sales Discount Percentage',
        'data_type'          => 'number',
        'current_value_json' => '{"percentage":"0.00"}',
        'parameters_json'    => '{"percentage":"decimal"}',
    ]);

    $ensureRule([
        'rule_code'          => 'INV_GUEST_SALE_ALLOWED',
        'category'           => 'inventory',
        'name_ar'            => 'السماح بالبيع للزوار',
        'name_en'            => 'Allow Guest Sales',
        'data_type'          => 'boolean',
        'current_value_json' => '{"value":true}',
        'parameters_json'    => '{"value":"boolean"}',
    ]);

    $ensureRule([
        'rule_code'          => 'INV_VOID_SALE_WINDOW_HOURS',
        'category'           => 'inventory',
        'name_ar'            => 'ساعات مهلة إلغاء عملية البيع',
        'name_en'            => 'Void Sale Window Hours',
        'data_type'          => 'number',
        'current_value_json' => '{"hours":24}',
        'parameters_json'    => '{"hours":"integer"}',
    ]);
};
