<?php
declare(strict_types=1);

return function (\App\Core\Database $db): void {
    $types = [
        ['code' => 'GEN', 'name_ar' => 'يومية عامة',      'name_en' => 'General Journal',      'short_name_ar' => 'عامة',    'short_name_en' => 'GEN',  'number_prefix' => 'GEN-', 'is_default' => 1],
        ['code' => 'RCV', 'name_ar' => 'يومية مقبوضات',    'name_en' => 'Receipts Journal',     'short_name_ar' => 'مقبوضات', 'short_name_en' => 'RCV',  'number_prefix' => 'RCV-', 'is_auto_generated' => 1],
        ['code' => 'PAY', 'name_ar' => 'يومية مدفوعات',    'name_en' => 'Payments Journal',     'short_name_ar' => 'مدفوعات', 'short_name_en' => 'PAY',  'number_prefix' => 'PAY-', 'is_auto_generated' => 1],
        ['code' => 'PUR', 'name_ar' => 'يومية مشتريات',    'name_en' => 'Purchases Journal',    'short_name_ar' => 'مشتريات', 'short_name_en' => 'PUR',  'number_prefix' => 'PUR-', 'is_auto_generated' => 1],
        ['code' => 'SAL', 'name_ar' => 'يومية مبيعات',     'name_en' => 'Sales Journal',        'short_name_ar' => 'مبيعات',  'short_name_en' => 'SAL',  'number_prefix' => 'SAL-', 'is_auto_generated' => 1],
        ['code' => 'HR',  'name_ar' => 'يومية رواتب',      'name_en' => 'Payroll Journal',      'short_name_ar' => 'رواتب',   'short_name_en' => 'HR',   'number_prefix' => 'HR-',  'is_auto_generated' => 1],
        ['code' => 'ADJ', 'name_ar' => 'يومية تسويات',     'name_en' => 'Adjustments Journal',  'short_name_ar' => 'تسويات',  'short_name_en' => 'ADJ',  'number_prefix' => 'ADJ-'],
        ['code' => 'OPN', 'name_ar' => 'قيد افتتاحي',      'name_en' => 'Opening Entry',        'short_name_ar' => 'افتتاحي', 'short_name_en' => 'OPN',  'number_prefix' => 'OPN-', 'is_auto_generated' => 1],
        ['code' => 'CLS', 'name_ar' => 'قيد إقفال',        'name_en' => 'Closing Entry',        'short_name_ar' => 'إقفال',   'short_name_en' => 'CLS',  'number_prefix' => 'CLS-', 'is_auto_generated' => 1],
    ];

    $now = date('Y-m-d H:i:s');

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

        $db->insert('journal_types', [
            'code'              => $type['code'],
            'name_ar'           => $type['name_ar'],
            'name_en'           => $type['name_en'],
            'short_name_ar'     => $type['short_name_ar'] ?? null,
            'short_name_en'     => $type['short_name_en'] ?? null,
            'numbering_method'  => 'yearly',
            'number_prefix'     => $type['number_prefix'] ?? null,
            'number_length'     => 6,
            'is_auto_generated' => (int) ($type['is_auto_generated'] ?? 0),
            'is_default'        => (int) ($type['is_default'] ?? 0),
            'is_active'         => 1,
            'created_at'        => $now,
            'updated_at'        => $now,
        ]);
    }
};
