<?php
declare(strict_types=1);

use App\Core\Database;

/**
 * Egyptian Standard Chart of Accounts — Full 5-Level Hierarchy.
 *
 * Follows the PDF (دليل الحسابات) EXACTLY for codes 1, 2, 3.
 * Revenue section (code 4) is customized for club/sports facility operations.
 *
 * Hierarchy:
 *   L1 (عام/الأول)    = 1 digit   (1, 2, 3, 4)
 *   L2 (عام/الثاني)   = 2 digits  (11, 12, 21, 22, 23, 31, 32, 33, 34, 41, 42)
 *   L3 (مساعد/الثالث) = 4 digits  (1101, 1202, 1206, 2101, 2306, 3101, 4101...)
 *   L4 (فرعي/الرابع)  = 6 digits  (110101, 120601, 230601, 310101...)
 *   L5 (جزئي/الخامس) = 8 digits  (11010101, 12060101, 23060101...)
 *
 * Strategy:
 *  1. Deactivate ALL old accounts (preserves FK integrity for journal entries)
 *  2. Insert complete new chart following Egyptian standard numbering
 *  3. Idempotent: checks account_code existence before insert
 */
return function (Database $db): void {

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

    // ─────────────────────────────────────────────────────────────
    // Step 1: Deactivate old accounts (4-digit codes from old system)
    // ─────────────────────────────────────────────────────────────
    $db->query(
        "UPDATE chart_of_accounts SET is_active = 0, updated_at = ? WHERE is_active = 1",
        [$now]
    );

    // ─────────────────────────────────────────────────────────────
    // Step 2: Full Egyptian Standard Chart — exact PDF structure
    // ─────────────────────────────────────────────────────────────
    $accounts = [

        // ══════════════════════════════════════════════════════════════════
        // 1 — الأصول (ASSETS)
        // ══════════════════════════════════════════════════════════════════
        ['account_code' => '1',    'name_ar' => 'الأصول',    'name_en' => 'Assets',    'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => null, 'level' => 1, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],

        // ── 11 — الأصول الثابتة (Fixed Assets) ──
        ['account_code' => '11',     'name_ar' => 'الأصول الثابتة',                'name_en' => 'Fixed Assets',                  'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '1',    'level' => 2, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '1101',   'name_ar' => 'أصول ثابتة ملموسة',             'name_en' => 'Tangible Fixed Assets',         'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '11',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '110101', 'name_ar' => 'أراضي',                         'name_en' => 'Land',                          'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '1101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110102', 'name_ar' => 'مباني ومنشآت',                   'name_en' => 'Buildings & Structures',        'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '1101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110103', 'name_ar' => 'آلات ومعدات',                    'name_en' => 'Machinery & Equipment',         'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '1101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110104', 'name_ar' => 'وسائل نقل وانتقال',              'name_en' => 'Vehicles',                      'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '1101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110105', 'name_ar' => 'عدد وأدوات',                     'name_en' => 'Tools & Instruments',           'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '1101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110106', 'name_ar' => 'أجهزة كمبيوتر',                  'name_en' => 'Computers',                     'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '1101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110107', 'name_ar' => 'تجهيزات وديكورات',               'name_en' => 'Fixtures & Decorations',        'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '1101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1102',   'name_ar' => 'أصول ثابتة غير ملموسة',          'name_en' => 'Intangible Assets',             'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '11',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '110201', 'name_ar' => 'شهرة المحل',                     'name_en' => 'Goodwill',                      'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '1102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110202', 'name_ar' => 'علامات تجارية',                  'name_en' => 'Trademarks',                    'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '1102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110203', 'name_ar' => 'برامج حاسب آلي',                 'name_en' => 'Software',                      'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '1102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1103',   'name_ar' => 'مشروعات تحت التنفيذ',            'name_en' => 'Projects Under Construction',   'account_type' => 'asset', 'account_nature' => 'debit',  'parent_code' => '11',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1104',   'name_ar' => 'مجمع الإهلاك',                   'name_en' => 'Accumulated Depreciation',      'account_type' => 'asset', 'account_nature' => 'credit', 'parent_code' => '11',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '110401', 'name_ar' => 'مجمع إهلاك مباني',               'name_en' => 'Accum. Depr. Buildings',        'account_type' => 'asset', 'account_nature' => 'credit', 'parent_code' => '1104', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110402', 'name_ar' => 'مجمع إهلاك آلات ومعدات',         'name_en' => 'Accum. Depr. Machinery',        'account_type' => 'asset', 'account_nature' => 'credit', 'parent_code' => '1104', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110403', 'name_ar' => 'مجمع إهلاك وسائل نقل',           'name_en' => 'Accum. Depr. Vehicles',         'account_type' => 'asset', 'account_nature' => 'credit', 'parent_code' => '1104', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110404', 'name_ar' => 'مجمع إهلاك عدد وأدوات',          'name_en' => 'Accum. Depr. Tools',            'account_type' => 'asset', 'account_nature' => 'credit', 'parent_code' => '1104', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110405', 'name_ar' => 'مجمع إهلاك أجهزة كمبيوتر',       'name_en' => 'Accum. Depr. Computers',        'account_type' => 'asset', 'account_nature' => 'credit', 'parent_code' => '1104', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '110406', 'name_ar' => 'مجمع إهلاك تجهيزات وديكورات',    'name_en' => 'Accum. Depr. Fixtures',         'account_type' => 'asset', 'account_nature' => 'credit', 'parent_code' => '1104', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // ── 12 — الأصول المتداولة (Current Assets) ──
        ['account_code' => '12',     'name_ar' => 'الأصول المتداولة',               'name_en' => 'Current Assets',                'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1',    'level' => 2, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],

        // 1201 — استثمارات متداولة
        ['account_code' => '1201',   'name_ar' => 'استثمارات متداولة',              'name_en' => 'Current Investments',           'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '12',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '120101', 'name_ar' => 'أسهم وسندات',                    'name_en' => 'Stocks & Bonds',                'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1201', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120102', 'name_ar' => 'ودائع قصيرة الأجل',              'name_en' => 'Short-term Deposits',           'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1201', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // 1202 — المخزون
        ['account_code' => '1202',   'name_ar' => 'المخزون',                        'name_en' => 'Inventory',                     'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '12',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '120201', 'name_ar' => 'مخزون مواد خام',                  'name_en' => 'Raw Materials Inventory',       'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1202', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120202', 'name_ar' => 'مخزون إنتاج تحت التشغيل',        'name_en' => 'Work-in-Progress Inventory',    'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1202', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120203', 'name_ar' => 'مخزون إنتاج تام',                'name_en' => 'Finished Goods Inventory',      'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1202', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120204', 'name_ar' => 'مخزون قطع غيار',                  'name_en' => 'Spare Parts Inventory',         'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1202', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120205', 'name_ar' => 'مخزون مواد تعبئة وتغليف',         'name_en' => 'Packing Materials Inventory',   'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1202', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120206', 'name_ar' => 'مخزون مواد ومهمات',               'name_en' => 'Supplies Inventory',            'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1202', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120207', 'name_ar' => 'بضاعة بالطريق',                  'name_en' => 'Goods in Transit',              'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1202', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120208', 'name_ar' => 'اعتمادات مستندية مخزون',          'name_en' => 'LC for Inventory',              'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1202', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120209', 'name_ar' => 'مخزون بضائع بغرض البيع',          'name_en' => 'Merchandise Inventory',         'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1202', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],

        // 1203 — عملاء وأوراق قبض
        ['account_code' => '1203',   'name_ar' => 'عملاء وأوراق قبض',               'name_en' => 'Customers & Notes Receivable',  'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '12',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '120301', 'name_ar' => 'العملاء',                        'name_en' => 'Accounts Receivable',           'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1203', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '120302', 'name_ar' => 'أوراق قبض',                      'name_en' => 'Notes Receivable',              'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1203', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120303', 'name_ar' => 'شيكات تحت التحصيل',              'name_en' => 'Checks Under Collection',       'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1203', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120304', 'name_ar' => 'مخصص ديون مشكوك فيها',           'name_en' => 'Allowance for Doubtful Debts',  'account_type' => 'asset', 'account_nature' => 'credit', 'parent_code' => '1203', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // 1204 — مدينون وأرصدة مدينة
        ['account_code' => '1204',   'name_ar' => 'مدينون وأرصدة مدينة',            'name_en' => 'Debtors & Debit Balances',      'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '12',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '120401', 'name_ar' => 'مدينون متنوعون',                 'name_en' => 'Sundry Debtors',                'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1204', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120402', 'name_ar' => 'سلف عاملين',                     'name_en' => 'Employee Advances',             'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1204', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '120403', 'name_ar' => 'عهد مستديمة',                    'name_en' => 'Permanent Custody',             'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1204', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120404', 'name_ar' => 'تأمينات لدى الغير',              'name_en' => 'Deposits with Others',          'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1204', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120405', 'name_ar' => 'مصروفات مقدمة',                  'name_en' => 'Prepaid Expenses',              'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1204', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120406', 'name_ar' => 'إيرادات مستحقة',                 'name_en' => 'Accrued Revenue',               'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1204', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120407', 'name_ar' => 'حسابات التسوية المدينة',          'name_en' => 'Debit Settlement Accounts',     'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1204', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120408', 'name_ar' => 'ضريبة مدخلات (مشتريات)',         'name_en' => 'Input VAT',                     'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1204', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],

        // 1205 — أرصدة مدينة أخرى
        ['account_code' => '1205',   'name_ar' => 'أرصدة مدينة أخرى',              'name_en' => 'Other Debit Balances',          'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '12',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '120501', 'name_ar' => 'حسابات التحصيل',                 'name_en' => 'Collection Accounts',           'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1205', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120502', 'name_ar' => 'خطابات ضمان',                    'name_en' => 'Letters of Guarantee',          'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1205', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // 1206 — النقدية ومافي حكمها
        ['account_code' => '1206',     'name_ar' => 'النقدية ومافي حكمها',          'name_en' => 'Cash & Cash Equivalents',       'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '12',     'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '120601',   'name_ar' => 'الصندوق',                      'name_en' => 'Cash on Hand',                  'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1206',   'level' => 4, 'level_name' => 'فرعي', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '12060101', 'name_ar' => 'الصندوق بالجنيه المصري',       'name_en' => 'Cash Box - EGP',                'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '120601', 'level' => 5, 'level_name' => 'جزئي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '12060102', 'name_ar' => 'الصندوق بالعملات الأجنبية',    'name_en' => 'Cash Box - Foreign Currency',   'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '120601', 'level' => 5, 'level_name' => 'جزئي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120602',   'name_ar' => 'حسابات البنوك',                'name_en' => 'Bank Accounts',                 'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1206',   'level' => 4, 'level_name' => 'فرعي', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '12060201', 'name_ar' => 'البنك الرئيسي - جنيه مصري',   'name_en' => 'Main Bank - EGP',               'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '120602', 'level' => 5, 'level_name' => 'جزئي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '12060202', 'name_ar' => 'حساب بنكي - عملة أجنبية',     'name_en' => 'Bank Account - Foreign',        'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '120602', 'level' => 5, 'level_name' => 'جزئي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '120603',   'name_ar' => 'شيكات تحت الإيداع',            'name_en' => 'Checks Pending Deposit',        'account_type' => 'asset', 'account_nature' => 'debit', 'parent_code' => '1206',   'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // ══════════════════════════════════════════════════════════════════
        // 2 — حقوق الملكية والإلتزامات (EQUITY & LIABILITIES)
        // ══════════════════════════════════════════════════════════════════
        ['account_code' => '2', 'name_ar' => 'حقوق الملكية والإلتزامات', 'name_en' => 'Equity & Liabilities', 'account_type' => 'equity', 'account_nature' => 'credit', 'parent_code' => null, 'level' => 1, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],

        // ── 21 — حقوق الملكية (EQUITY) ──
        ['account_code' => '21',     'name_ar' => 'حقوق الملكية',                   'name_en' => 'Equity',                        'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '2',    'level' => 2, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '2101',   'name_ar' => 'رأس المال المدفوع',              'name_en' => 'Paid-in Capital',               'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '21',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '2102',   'name_ar' => 'الاحتياطيات',                    'name_en' => 'Reserves',                      'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '21',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '210201', 'name_ar' => 'أرباح مرحلة',                    'name_en' => 'Retained Earnings',             'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '2102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '210202', 'name_ar' => 'احتياطي قانوني',                 'name_en' => 'Legal Reserve',                 'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '2102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '210203', 'name_ar' => 'احتياطي نظامي',                  'name_en' => 'Statutory Reserve',             'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '2102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '210204', 'name_ar' => 'احتياطي عام',                    'name_en' => 'General Reserve',               'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '2102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2103',   'name_ar' => 'أرباح (خسائر) العام',            'name_en' => 'Current Year Profit/Loss',      'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '21',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],

        // ── 22 — الإلتزامات الغير المتداولة (NON-CURRENT LIABILITIES) ──
        ['account_code' => '22',     'name_ar' => 'الإلتزامات الغير المتداولة',     'name_en' => 'Non-Current Liabilities',       'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2',    'level' => 2, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '2201',   'name_ar' => 'قروض طويلة الأجل',               'name_en' => 'Long-term Loans',               'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '22',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '220101', 'name_ar' => 'قروض من بنوك عملة محلية',        'name_en' => 'Bank Loans - Local',            'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2201', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '220102', 'name_ar' => 'قروض من بنوك عملة أجنبية',       'name_en' => 'Bank Loans - Foreign',          'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2201', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2202',   'name_ar' => 'قروض من مؤسسات مالية',           'name_en' => 'Financial Institution Loans',   'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '22',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2203',   'name_ar' => 'قروض من جهات أخرى',              'name_en' => 'Loans from Other Parties',      'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '22',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2204',   'name_ar' => 'سندات',                          'name_en' => 'Bonds Payable',                 'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '22',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2205',   'name_ar' => 'مخصصات طويلة الأجل',             'name_en' => 'Long-term Provisions',          'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '22',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '220501', 'name_ar' => 'مخصص مكافأة نهاية خدمة',         'name_en' => 'End of Service Provision',      'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2205', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2206',   'name_ar' => 'التزامات طويلة الأجل أخرى',      'name_en' => 'Other Long-term Liabilities',   'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '22',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],

        // ── 23 — الإلتزامات المتداولة (CURRENT LIABILITIES) ──
        ['account_code' => '23',     'name_ar' => 'الإلتزامات المتداولة',           'name_en' => 'Current Liabilities',           'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2',    'level' => 2, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '2301',   'name_ar' => 'بنوك دائنة (سحب على المكشوف)',   'name_en' => 'Bank Overdraft',                'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '23',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2302',   'name_ar' => 'قروض قصيرة الأجل',               'name_en' => 'Short-term Loans',              'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '23',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2303',   'name_ar' => 'الجزء المتداول من القروض',       'name_en' => 'Current Portion of Long-term',  'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '23',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2304',   'name_ar' => 'سندات وأذون خزانة',              'name_en' => 'Treasury Bills & Bonds',        'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '23',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2305',   'name_ar' => 'أوراق دفع',                      'name_en' => 'Notes Payable',                 'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '23',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2306',   'name_ar' => 'الموردون',                       'name_en' => 'Suppliers',                     'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '23',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '230601', 'name_ar' => 'الموردون',                       'name_en' => 'Accounts Payable',              'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2306', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '230602', 'name_ar' => 'موردون - خدمات',                 'name_en' => 'Service Suppliers',             'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2306', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2307',   'name_ar' => 'دائنون متنوعون',                 'name_en' => 'Sundry Creditors',              'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '23',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2308',   'name_ar' => 'أرصدة دائنة أخرى',              'name_en' => 'Other Credit Balances',         'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '23',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '230801', 'name_ar' => 'تأمينات للغير',                  'name_en' => 'Deposits from Others',          'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2308', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '230802', 'name_ar' => 'إيرادات محصلة مقدما',            'name_en' => 'Unearned Revenue',              'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2308', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '230803', 'name_ar' => 'أقساط قروض مستحقة',              'name_en' => 'Loan Installments Due',         'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2308', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '230804', 'name_ar' => 'جاري مصلحة الضرائب',             'name_en' => 'Tax Authority Payable',         'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2308', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '230805', 'name_ar' => 'الهيئة العامة للتأمين الصحي',    'name_en' => 'Health Insurance Authority',    'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2308', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '230806',   'name_ar' => 'الهيئة القومية للتأمينات',     'name_en' => 'Social Insurance Authority',    'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2308',   'level' => 4, 'level_name' => 'فرعي', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '23080601', 'name_ar' => 'الهيئة القومية للتأمينات الاجتماعية', 'name_en' => 'Social Insurance Payable', 'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '230806', 'level' => 5, 'level_name' => 'جزئي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '230807', 'name_ar' => 'صناديق خاصة',                    'name_en' => 'Special Funds',                 'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2308', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '230808', 'name_ar' => 'مخصص ضريبة الدخل',               'name_en' => 'Income Tax Provision',          'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2308', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '230809', 'name_ar' => 'إيرادات مدفوعة مقدما',           'name_en' => 'Deferred Revenue',              'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2308', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '230810', 'name_ar' => 'مصروفات مستحقة',                 'name_en' => 'Accrued Expenses',              'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2308', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],

        // ══════════════════════════════════════════════════════════════════
        // 3 — التكاليف والمصروفات (COSTS & EXPENSES)
        // ══════════════════════════════════════════════════════════════════
        ['account_code' => '3', 'name_ar' => 'التكاليف والمصروفات', 'name_en' => 'Costs & Expenses', 'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => null, 'level' => 1, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],

        // ── 31 — تكاليف النشاط (Cost of Operations) ──
        ['account_code' => '31',     'name_ar' => 'تكاليف النشاط',                  'name_en' => 'Cost of Operations',            'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3',    'level' => 2, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],

        // 3101 — الاجور والمرتبات (تكاليف النشاط)
        ['account_code' => '3101',   'name_ar' => 'الاجور والمرتبات',               'name_en' => 'Wages & Salaries - Operations', 'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '31',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '310101', 'name_ar' => 'الاجور الاساسية',                'name_en' => 'Basic Wages',                   'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '310102', 'name_ar' => 'الأجور الإضافية',                'name_en' => 'Overtime Wages',                'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '310103', 'name_ar' => 'حصة الشركة في التأمينات',        'name_en' => 'Employer Social Insurance',     'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '310104', 'name_ar' => 'بدلات نقدية',                    'name_en' => 'Cash Allowances',               'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '310105', 'name_ar' => 'مزايا عينية',                    'name_en' => 'Benefits in Kind',              'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '310106', 'name_ar' => 'مكافآت وحوافز',                  'name_en' => 'Bonuses & Incentives',          'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // 3102 — مستلزمات سلعية
        ['account_code' => '3102',   'name_ar' => 'مستلزمات سلعية',                 'name_en' => 'Commodity Requirements',        'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '31',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '310201', 'name_ar' => 'مواد خام ومهمات',                'name_en' => 'Raw Materials & Supplies',      'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '310202', 'name_ar' => 'قطع غيار ووقود',                  'name_en' => 'Spare Parts & Fuel',            'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '310203', 'name_ar' => 'مواد تعبئة وتغليف',              'name_en' => 'Packing Materials',             'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // 3103 — مستلزمات خدمية
        ['account_code' => '3103',   'name_ar' => 'مستلزمات خدمية',                 'name_en' => 'Service Requirements',          'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '31',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '310301', 'name_ar' => 'مصروفات نقل وانتقال',            'name_en' => 'Transportation',                'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3103', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '310302', 'name_ar' => 'مصروفات إيجار - نشاط',           'name_en' => 'Rent - Operations',             'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3103', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '310303', 'name_ar' => 'مصروفات صيانة - نشاط',           'name_en' => 'Maintenance - Operations',      'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3103', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '310304', 'name_ar' => 'مصروفات كهرباء ومياه - نشاط',    'name_en' => 'Utilities - Operations',        'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3103', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '310305', 'name_ar' => 'مصروفات تأمين - نشاط',           'name_en' => 'Insurance - Operations',        'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3103', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // 3104-3171 — other operating codes (abbreviated)
        ['account_code' => '3104',   'name_ar' => 'مصروفات إدارية - نشاط',          'name_en' => 'Admin Expenses - Operations',   'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '31',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '310401', 'name_ar' => 'مطبوعات وأدوات كتابية',          'name_en' => 'Stationery - Operations',       'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3104', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '310402', 'name_ar' => 'مصروفات ضيافة - نشاط',           'name_en' => 'Hospitality - Operations',      'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3104', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // 3172 — تكلفة بضائع بغرض البيع (COGS)
        ['account_code' => '3172',   'name_ar' => 'مخزون بضائع بغرض البيع',         'name_en' => 'Cost of Goods Sold',            'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '31',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 1],

        // ── 32 — مصروفات تسويقية وبيعية (Selling & Marketing) ──
        ['account_code' => '32',     'name_ar' => 'مصروفات تسويقية وبيعية',         'name_en' => 'Selling & Marketing Expenses',  'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3',    'level' => 2, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '3201',   'name_ar' => 'الاجور والمرتبات - بيعية',       'name_en' => 'Wages - Selling',               'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '32',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '320101', 'name_ar' => 'أجور موظفي المبيعات',            'name_en' => 'Sales Staff Wages',             'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3201', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3202',   'name_ar' => 'عمولات بيعية',                   'name_en' => 'Sales Commissions',             'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '32',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3203',   'name_ar' => 'مصروفات دعاية وإعلان',           'name_en' => 'Advertising & Publicity',       'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '32',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3204',   'name_ar' => 'مصروفات نقل مبيعات',             'name_en' => 'Sales Transportation',          'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '32',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3205',   'name_ar' => 'مصروفات تسويقية متنوعة',         'name_en' => 'Other Marketing Expenses',      'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '32',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],

        // ── 33 — مصروفات عمومية وإدارية (General & Administrative) ──
        ['account_code' => '33',     'name_ar' => 'مصروفات عمومية وإدارية',         'name_en' => 'General & Admin Expenses',      'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3',    'level' => 2, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],

        // 3301 — الاجور والمرتبات - عمومي
        ['account_code' => '3301',   'name_ar' => 'الاجور والمرتبات - عمومي',       'name_en' => 'Wages - Admin',                 'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '330101', 'name_ar' => 'أجور إدارية',                    'name_en' => 'Admin Wages',                   'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3301', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '330102', 'name_ar' => 'تأمينات - إداري',                'name_en' => 'Insurance - Admin',             'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3301', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // 3302-3313 — various G&A expenses
        ['account_code' => '3302',   'name_ar' => 'مستلزمات سلعية - عمومي',         'name_en' => 'Commodity - Admin',             'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3303',   'name_ar' => 'مستلزمات خدمية - عمومي',         'name_en' => 'Services - Admin',              'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '330301', 'name_ar' => 'مصروفات نقل - إداري',            'name_en' => 'Transportation - Admin',        'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3303', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '330302', 'name_ar' => 'مصروفات إيجار - إداري',          'name_en' => 'Rent - Admin',                  'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3303', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '330303', 'name_ar' => 'مصروفات صيانة - إداري',          'name_en' => 'Maintenance - Admin',           'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3303', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '330304', 'name_ar' => 'كهرباء ومياه - إداري',           'name_en' => 'Utilities - Admin',             'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3303', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '330305', 'name_ar' => 'اتصالات وإنترنت - إداري',        'name_en' => 'Communications - Admin',        'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3303', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3304',   'name_ar' => 'مصروفات مطبوعات وقرطاسية',       'name_en' => 'Printing & Stationery',         'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3305',   'name_ar' => 'مصروفات ضيافة - عمومي',          'name_en' => 'Hospitality - Admin',           'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3306',   'name_ar' => 'مصروفات سفر - عمومي',            'name_en' => 'Travel - Admin',                'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3307',   'name_ar' => 'مصروفات تدريب',                  'name_en' => 'Training Expenses',             'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3308',   'name_ar' => 'رسوم حكومية',                    'name_en' => 'Government Fees',               'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3309',   'name_ar' => 'استشارات قانونية ومهنية',         'name_en' => 'Legal & Professional Fees',     'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3310',   'name_ar' => 'مصروفات أمن وحراسة',             'name_en' => 'Security Expenses',             'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3311',   'name_ar' => 'مصروفات نظافة',                  'name_en' => 'Cleaning Expenses',             'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3312',   'name_ar' => 'مصروفات تأمين ممتلكات',          'name_en' => 'Property Insurance',            'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3313',   'name_ar' => 'مصروفات متنوعة - عمومي',         'name_en' => 'Misc. Expenses - Admin',        'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],

        // 3314 — عمولة ومصروفات بنكية
        ['account_code' => '3314',   'name_ar' => 'عمولة ومصروفات بنكية',           'name_en' => 'Bank Fees & Commissions',       'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '331401', 'name_ar' => 'عمولات بنكية',                   'name_en' => 'Bank Commissions',              'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3314', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '331402', 'name_ar' => 'مصروفات بنكية',                  'name_en' => 'Bank Charges',                  'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3314', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],

        // 3315 — مصروفات ديون معدومة
        ['account_code' => '3315',   'name_ar' => 'مصروفات ديون معدومة',            'name_en' => 'Bad Debts Expense',             'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],

        // 3316 — الاهلاكات
        ['account_code' => '3316',   'name_ar' => 'الاهلاكات',                      'name_en' => 'Depreciation',                  'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 1],

        // 3317 — مخصصات
        ['account_code' => '3317',   'name_ar' => 'مخصصات',                         'name_en' => 'Provisions',                    'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '33',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],

        // ── 34 — مصروفات تمويلية (Financing Expenses) ──
        ['account_code' => '34',     'name_ar' => 'مصروفات تمويلية',                'name_en' => 'Financing Expenses',            'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3',    'level' => 2, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '3401',   'name_ar' => 'فوائد مدينة',                    'name_en' => 'Interest Expense',              'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '34',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '340101', 'name_ar' => 'فوائد قروض',                     'name_en' => 'Loan Interest',                 'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3401', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '340102', 'name_ar' => 'فوائد سحب على المكشوف',          'name_en' => 'Overdraft Interest',            'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '3401', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3402',   'name_ar' => 'فروق عملة',                      'name_en' => 'Foreign Exchange Loss',         'account_type' => 'expense', 'account_nature' => 'debit', 'parent_code' => '34',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],

        // ══════════════════════════════════════════════════════════════════
        // 4 — الإيرادات (REVENUE) — CUSTOMIZED FOR CLUB
        // ══════════════════════════════════════════════════════════════════
        ['account_code' => '4', 'name_ar' => 'الإيرادات', 'name_en' => 'Revenue', 'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => null, 'level' => 1, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],

        // ── 41 — إيرادات النشاط (Operating Revenue) ──
        ['account_code' => '41',     'name_ar' => 'إيرادات النشاط',                 'name_en' => 'Operating Revenue',             'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4',    'level' => 2, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 1],

        // 4101 — إيرادات العضويات
        ['account_code' => '4101',   'name_ar' => 'إيرادات العضويات',               'name_en' => 'Membership Revenue',            'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '41',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '410101', 'name_ar' => 'رسوم استمارات',                  'name_en' => 'Form Fee Revenue',              'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '410102', 'name_ar' => 'قيمة عضوية',                     'name_en' => 'Membership Value Revenue',      'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '410103', 'name_ar' => 'رسوم إضافة ملحقين',              'name_en' => 'Dependent Addition Revenue',    'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4101', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],

        // 4102 — إيرادات الاشتراكات
        ['account_code' => '4102',   'name_ar' => 'إيرادات الاشتراكات',             'name_en' => 'Subscription Revenue',          'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '41',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '410201', 'name_ar' => 'اشتراك سنوي',                    'name_en' => 'Annual Subscription',           'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '410202', 'name_ar' => 'رسوم تنمية',                     'name_en' => 'Development Fee',               'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4102', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],

        // 4103 — إيرادات التحويلات والحالات
        ['account_code' => '4103',   'name_ar' => 'إيرادات التحويلات والحالات',     'name_en' => 'Transfer & Cases Revenue',      'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '41',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '410301', 'name_ar' => 'رسوم فصل',                       'name_en' => 'Separation Fee Revenue',        'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4103', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '410302', 'name_ar' => 'رسوم طلاق',                      'name_en' => 'Divorce Fee Revenue',           'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4103', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '410303', 'name_ar' => 'رسوم نقل وفاة',                  'name_en' => 'Death Transfer Fee',            'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4103', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '410304', 'name_ar' => 'رسوم تنازل',                     'name_en' => 'Waiver Fee Revenue',            'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4103', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '410305', 'name_ar' => 'تحويل رياضي',                    'name_en' => 'Sports Conversion Revenue',     'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4103', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],

        // 4104 — إيرادات عضويات خاصة
        ['account_code' => '4104',   'name_ar' => 'إيرادات عضويات خاصة',            'name_en' => 'Special Membership Revenue',    'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '41',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '410401', 'name_ar' => 'عضوية موسمية',                   'name_en' => 'Seasonal Membership',           'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4104', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '410402', 'name_ar' => 'بدل فاقد كارنيه',                'name_en' => 'Carnet Replacement',            'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4104', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '410403', 'name_ar' => 'عضوية أجنبية',                   'name_en' => 'Foreign Membership',            'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4104', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // 4105 — إيرادات الأنشطة والرياضة
        ['account_code' => '4105',   'name_ar' => 'إيرادات الأنشطة والرياضة',       'name_en' => 'Activities & Sports Revenue',   'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '41',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '410501', 'name_ar' => 'اشتراكات أنشطة',                 'name_en' => 'Activity Subscriptions',        'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4105', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '410502', 'name_ar' => 'أكاديميات رياضية',               'name_en' => 'Sports Academies Revenue',      'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4105', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 1],

        // 4106-4110 — single-level revenue accounts
        ['account_code' => '4106',   'name_ar' => 'إيرادات الغرامات',               'name_en' => 'Fine Revenue',                  'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '41',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '4107',   'name_ar' => 'إيرادات المبيعات',               'name_en' => 'Sales Revenue',                 'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '41',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '4108',   'name_ar' => 'إيرادات الإيجارات',              'name_en' => 'Rental Revenue',                'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '41',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],

        // 4109 — إيرادات الأقساط
        ['account_code' => '4109',   'name_ar' => 'إيرادات الأقساط',                'name_en' => 'Installment Revenue',           'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '41',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '410901', 'name_ar' => 'مقدمات تقسيط',                   'name_en' => 'Down Payment Revenue',          'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4109', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '410902', 'name_ar' => 'أقساط شهرية',                    'name_en' => 'Monthly Installment Revenue',   'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4109', 'level' => 4, 'level_name' => 'فرعي', 'is_header' => 0, 'is_system' => 0],

        // 4110 — إيرادات خدمات (catch-all)
        ['account_code' => '4110',   'name_ar' => 'إيرادات خدمات',                  'name_en' => 'Service Revenue',               'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '41',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 1],

        // ── 42 — إيرادات أخرى (Other Revenue) ──
        ['account_code' => '42',     'name_ar' => 'إيرادات أخرى',                   'name_en' => 'Other Revenue',                 'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '4',    'level' => 2, 'level_name' => 'عام', 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '4201',   'name_ar' => 'أرباح بيع أصول',                 'name_en' => 'Gain on Asset Disposal',        'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '42',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '4202',   'name_ar' => 'إعانات ومنح',                    'name_en' => 'Grants & Subsidies',            'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '42',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '4203',   'name_ar' => 'أرباح فروق عملة',                'name_en' => 'Foreign Exchange Gain',         'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '42',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '4204',   'name_ar' => 'إيرادات متنوعة',                 'name_en' => 'Miscellaneous Revenue',         'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '42',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '4205',   'name_ar' => 'فوائد دائنة',                    'name_en' => 'Bank Interest Income',          'account_type' => 'revenue', 'account_nature' => 'credit', 'parent_code' => '42',   'level' => 3, 'level_name' => 'مساعد', 'is_header' => 0, 'is_system' => 1],
    ];

    // ─────────────────────────────────────────────────────────────
    // Step 3: Insert accounts with parent resolution
    // ─────────────────────────────────────────────────────────────
    $codeToId = [];

    foreach ($accounts as $acct) {
        $existing = $db->selectOne(
            "SELECT id FROM chart_of_accounts WHERE account_code = ?",
            [$acct['account_code']]
        );
        if ($existing) {
            $codeToId[$acct['account_code']] = (int) $existing['id'];
            $db->query(
                "UPDATE chart_of_accounts SET is_active = 1, level_name = ?, updated_at = ? WHERE id = ?",
                [$acct['level_name'], $now, (int) $existing['id']]
            );
            continue;
        }

        $parentId = null;
        $parentCode = $acct['parent_code'] ?? null;
        if ($parentCode !== null && isset($codeToId[$parentCode])) {
            $parentId = $codeToId[$parentCode];
        }

        $levelName = $acct['level_name'];
        unset($acct['parent_code'], $acct['level_name']);

        $insertId = $db->insert('chart_of_accounts', array_merge($acct, [
            'parent_id'       => $parentId,
            'level_name'      => $levelName,
            'opening_balance' => '0.00',
            'current_balance' => '0.00',
            'is_active'       => 1,
            'is_archived'     => 0,
            'created_at'      => $now,
            'updated_at'      => $now,
        ]));

        $codeToId[$acct['account_code']] = $insertId;
    }
};
