<?php
declare(strict_types=1);

return function (\App\Core\Database $db): void {
    $types = [
        ['code' => 'sales_invoice', 'name_ar' => 'فاتورة مبيعات', 'name_en' => 'Sales Invoice', 'module' => 'sales', 'direction' => 'out', 'affects_stock' => 1, 'posts_to_gl' => 1, 'creates_ar' => 1, 'allows_discount' => 1, 'allows_tax' => 1, 'auto_number_prefix' => 'INV', 'sort_order' => 1],
        ['code' => 'sales_return', 'name_ar' => 'مرتجع مبيعات', 'name_en' => 'Sales Return', 'module' => 'sales', 'direction' => 'in', 'affects_stock' => 1, 'posts_to_gl' => 1, 'creates_ar' => 1, 'allows_discount' => 0, 'allows_tax' => 1, 'auto_number_prefix' => 'SRT', 'sort_order' => 2],
        ['code' => 'sales_quotation', 'name_ar' => 'عرض سعر مبيعات', 'name_en' => 'Sales Quotation', 'module' => 'sales', 'direction' => 'neutral', 'affects_stock' => 0, 'posts_to_gl' => 0, 'creates_ar' => 0, 'allows_discount' => 1, 'allows_tax' => 1, 'auto_number_prefix' => 'SQT', 'sort_order' => 3],
        ['code' => 'purchase_invoice', 'name_ar' => 'فاتورة مشتريات', 'name_en' => 'Purchase Invoice', 'module' => 'procurement', 'direction' => 'in', 'affects_stock' => 1, 'posts_to_gl' => 1, 'creates_ap' => 1, 'allows_discount' => 1, 'allows_tax' => 1, 'auto_number_prefix' => 'PIN', 'sort_order' => 1],
        ['code' => 'purchase_return', 'name_ar' => 'مرتجع مشتريات', 'name_en' => 'Purchase Return', 'module' => 'procurement', 'direction' => 'out', 'affects_stock' => 1, 'posts_to_gl' => 1, 'creates_ap' => 1, 'allows_discount' => 0, 'allows_tax' => 1, 'auto_number_prefix' => 'PRT', 'sort_order' => 2],
        ['code' => 'stock_in', 'name_ar' => 'إذن إضافة مخزون', 'name_en' => 'Stock In', 'module' => 'inventory', 'direction' => 'in', 'affects_stock' => 1, 'posts_to_gl' => 0, 'requires_approval' => 1, 'auto_number_prefix' => 'SIN', 'sort_order' => 1],
        ['code' => 'stock_out', 'name_ar' => 'إذن صرف مخزون', 'name_en' => 'Stock Out', 'module' => 'inventory', 'direction' => 'out', 'affects_stock' => 1, 'posts_to_gl' => 0, 'requires_approval' => 1, 'auto_number_prefix' => 'SOT', 'sort_order' => 2],
        ['code' => 'stock_transfer', 'name_ar' => 'تحويل بين مخازن', 'name_en' => 'Stock Transfer', 'module' => 'inventory', 'direction' => 'neutral', 'affects_stock' => 1, 'posts_to_gl' => 0, 'requires_approval' => 1, 'auto_number_prefix' => 'STR', 'sort_order' => 3],
        ['code' => 'stock_adjustment', 'name_ar' => 'تسوية مخزون', 'name_en' => 'Stock Adjustment', 'module' => 'inventory', 'direction' => 'neutral', 'affects_stock' => 1, 'posts_to_gl' => 1, 'requires_approval' => 1, 'auto_number_prefix' => 'ADJ', 'sort_order' => 4],
    ];

    foreach ($types as $type) {
        $existing = $db->selectOne("SELECT id FROM movement_types WHERE code = ?", [$type['code']]);
        if ($existing) continue;

        $db->insert('movement_types', array_merge([
            'creates_ar' => 0,
            'creates_ap' => 0,
            'requires_approval' => 0,
            'allows_discount' => 0,
            'allows_tax' => 0,
            'party_types' => null,
            'auto_number_next' => 1,
            'is_active' => 1,
        ], $type));
    }

    // Seed base currency
    $existing = $db->selectOne("SELECT id FROM currencies WHERE code = 'EGP'");
    if (!$existing) {
        $db->insert('currencies', [
            'code' => 'EGP',
            'name_ar' => 'جنيه مصري',
            'name_en' => 'Egyptian Pound',
            'symbol' => 'ج.م',
            'decimal_places' => 2,
            'is_base_currency' => 1,
            'is_active' => 1,
        ]);
        $db->insert('currencies', [
            'code' => 'USD',
            'name_ar' => 'دولار أمريكي',
            'name_en' => 'US Dollar',
            'symbol' => '$',
            'decimal_places' => 2,
            'is_base_currency' => 0,
            'is_active' => 1,
        ]);
        $db->insert('currencies', [
            'code' => 'EUR',
            'name_ar' => 'يورو',
            'name_en' => 'Euro',
            'symbol' => '€',
            'decimal_places' => 2,
            'is_base_currency' => 0,
            'is_active' => 1,
        ]);
        $db->insert('currencies', [
            'code' => 'SAR',
            'name_ar' => 'ريال سعودي',
            'name_en' => 'Saudi Riyal',
            'symbol' => 'ر.س',
            'decimal_places' => 2,
            'is_base_currency' => 0,
            'is_active' => 1,
        ]);
    }

    // Seed default overtime types
    $existing = $db->selectOne("SELECT id FROM hr_overtime_types LIMIT 1");
    if (!$existing) {
        $db->insert('hr_overtime_types', ['name_ar' => 'عمل إضافي عادي', 'name_en' => 'Normal Overtime', 'rate_multiplier' => '1.50', 'max_hours_per_day' => '4.00', 'max_hours_per_month' => '50.00']);
        $db->insert('hr_overtime_types', ['name_ar' => 'عمل إضافي إجازات', 'name_en' => 'Holiday Overtime', 'rate_multiplier' => '2.00', 'max_hours_per_day' => '8.00', 'max_hours_per_month' => '30.00']);
        $db->insert('hr_overtime_types', ['name_ar' => 'عمل إضافي ليلي', 'name_en' => 'Night Overtime', 'rate_multiplier' => '1.75', 'max_hours_per_day' => '6.00', 'max_hours_per_month' => '40.00']);
    }

    // Seed Egyptian insurance config (2024 rates)
    $existing = $db->selectOne("SELECT id FROM hr_insurance_config LIMIT 1");
    if (!$existing) {
        $db->insert('hr_insurance_config', [
            'effective_date' => '2024-01-01',
            'basic_salary_cap' => '12600.00',
            'variable_salary_cap' => '10080.00',
            'employer_basic_rate' => '0.1875',
            'employer_variable_rate' => '0.1575',
            'employee_basic_rate' => '0.1100',
            'employee_variable_rate' => '0.1100',
            'min_subscription_salary' => '2000.00',
            'is_active' => 1,
        ]);
    }

    // Seed Egyptian tax brackets (2024)
    $existing = $db->selectOne("SELECT id FROM hr_tax_brackets LIMIT 1");
    if (!$existing) {
        $brackets = [
            ['order' => 1, 'from' => '0', 'to' => '40000', 'rate' => '0.0000', 'exemption' => '20000'],
            ['order' => 2, 'from' => '40000', 'to' => '55000', 'rate' => '0.1000', 'exemption' => '0'],
            ['order' => 3, 'from' => '55000', 'to' => '70000', 'rate' => '0.1500', 'exemption' => '0'],
            ['order' => 4, 'from' => '70000', 'to' => '200000', 'rate' => '0.2000', 'exemption' => '0'],
            ['order' => 5, 'from' => '200000', 'to' => '400000', 'rate' => '0.2250', 'exemption' => '0'],
            ['order' => 6, 'from' => '400000', 'to' => '600000', 'rate' => '0.2500', 'exemption' => '0'],
            ['order' => 7, 'from' => '600000', 'to' => '99999999', 'rate' => '0.2750', 'exemption' => '0'],
        ];
        foreach ($brackets as $b) {
            $db->insert('hr_tax_brackets', [
                'bracket_order' => $b['order'],
                'from_amount' => $b['from'],
                'to_amount' => $b['to'],
                'rate' => $b['rate'],
                'annual_exemption' => $b['exemption'],
                'effective_date' => '2024-01-01',
                'is_active' => 1,
            ]);
        }
    }

    // Seed default shift
    $existing = $db->selectOne("SELECT id FROM hr_shifts LIMIT 1");
    if (!$existing) {
        $db->insert('hr_shifts', [
            'name_ar' => 'الوردية الصباحية',
            'name_en' => 'Morning Shift',
            'start_time' => '08:00:00',
            'end_time' => '16:00:00',
            'break_start' => '12:00:00',
            'break_end' => '13:00:00',
            'break_duration_minutes' => 60,
            'working_hours' => '7.00',
            'grace_period_minutes' => 15,
            'early_leave_threshold_minutes' => 15,
            'is_night_shift' => 0,
        ]);
        $db->insert('hr_shifts', [
            'name_ar' => 'الوردية المسائية',
            'name_en' => 'Evening Shift',
            'start_time' => '16:00:00',
            'end_time' => '00:00:00',
            'break_start' => '19:00:00',
            'break_end' => '19:30:00',
            'break_duration_minutes' => 30,
            'working_hours' => '7.50',
            'grace_period_minutes' => 10,
            'early_leave_threshold_minutes' => 10,
            'is_night_shift' => 1,
        ]);
    }
};
