<?php
declare(strict_types=1);

use App\Core\Database;

/**
 * Egyptian Standard Chart of Accounts - tailored for Club/Membership ERP.
 *
 * Structure: 4-digit codes
 *   1xxx = Assets
 *   2xxx = Liabilities
 *   3xxx = Equity
 *   4xxx = Revenue
 *   5xxx = Expenses
 *
 * Run: php cli.php seed
 */
return function (Database $db): void {

    $accounts = [
        // ══════════════════════════════════════════════════════════
        // 1. ASSETS
        // ══════════════════════════════════════════════════════════
        ['account_code' => '1000', 'name_ar' => 'الأصول',                    'name_en' => 'Assets',                     'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => null,   'level' => 1, 'is_header' => 1, 'is_system' => 1],

        // Current Assets
        ['account_code' => '1100', 'name_ar' => 'أصول متداولة',              'name_en' => 'Current Assets',             'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1000', 'level' => 2, 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '1101', 'name_ar' => 'نقدية بالصندوق',            'name_en' => 'Cash on Hand',               'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '1102', 'name_ar' => 'نقدية بالبنك',              'name_en' => 'Cash at Bank',               'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '1103', 'name_ar' => 'حسابات مدينة (مدينون)',     'name_en' => 'Accounts Receivable',        'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '1104', 'name_ar' => 'مخزون',                     'name_en' => 'Inventory',                  'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '1105', 'name_ar' => 'مصروفات مقدمة',             'name_en' => 'Prepaid Expenses',           'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1106', 'name_ar' => 'شيكات تحت التحصيل',         'name_en' => 'Checks Under Collection',    'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1107', 'name_ar' => 'سلف ومقدمات موظفين',        'name_en' => 'Employee Advances',          'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1108', 'name_ar' => 'أوراق قبض',                 'name_en' => 'Notes Receivable',           'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],

        // Fixed Assets
        ['account_code' => '1200', 'name_ar' => 'أصول ثابتة',                'name_en' => 'Fixed Assets',               'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1000', 'level' => 2, 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '1201', 'name_ar' => 'أراضي',                     'name_en' => 'Land',                       'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1202', 'name_ar' => 'مباني ومنشآت',              'name_en' => 'Buildings',                  'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1203', 'name_ar' => 'أثاث وتجهيزات',             'name_en' => 'Furniture & Fixtures',       'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1204', 'name_ar' => 'معدات وأجهزة',              'name_en' => 'Equipment',                  'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1205', 'name_ar' => 'سيارات ووسائل نقل',         'name_en' => 'Vehicles',                   'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1206', 'name_ar' => 'أجهزة حاسب آلي',            'name_en' => 'Computer Equipment',         'account_type' => 'asset',     'account_nature' => 'debit', 'parent_code' => '1200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '1290', 'name_ar' => 'مجمع الإهلاك',              'name_en' => 'Accumulated Depreciation',   'account_type' => 'asset',     'account_nature' => 'credit','parent_code' => '1200', 'level' => 3, 'is_header' => 0, 'is_system' => 1],

        // ══════════════════════════════════════════════════════════
        // 2. LIABILITIES
        // ══════════════════════════════════════════════════════════
        ['account_code' => '2000', 'name_ar' => 'الخصوم',                    'name_en' => 'Liabilities',                'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => null,   'level' => 1, 'is_header' => 1, 'is_system' => 1],

        // Current Liabilities
        ['account_code' => '2100', 'name_ar' => 'خصوم متداولة',              'name_en' => 'Current Liabilities',        'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2000', 'level' => 2, 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '2101', 'name_ar' => 'حسابات دائنة (دائنون)',     'name_en' => 'Accounts Payable',           'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '2102', 'name_ar' => 'مصروفات مستحقة',            'name_en' => 'Accrued Expenses',           'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2103', 'name_ar' => 'ضرائب مستحقة',              'name_en' => 'Tax Payable',                'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '2104', 'name_ar' => 'تأمينات اجتماعية مستحقة',   'name_en' => 'Insurance Payable',          'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '2105', 'name_ar' => 'إيرادات مؤجلة',             'name_en' => 'Deferred Revenue',           'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '2106', 'name_ar' => 'أوراق دفع',                 'name_en' => 'Notes Payable',              'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '2107', 'name_ar' => 'تأمينات عضويات مستحقة',     'name_en' => 'Membership Deposits',        'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],

        // Long-term Liabilities
        ['account_code' => '2200', 'name_ar' => 'خصوم طويلة الأجل',          'name_en' => 'Long-term Liabilities',      'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2000', 'level' => 2, 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '2201', 'name_ar' => 'قروض طويلة الأجل',          'name_en' => 'Long-term Loans',            'account_type' => 'liability', 'account_nature' => 'credit', 'parent_code' => '2200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],

        // ══════════════════════════════════════════════════════════
        // 3. EQUITY
        // ══════════════════════════════════════════════════════════
        ['account_code' => '3000', 'name_ar' => 'حقوق الملكية',              'name_en' => 'Equity',                     'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => null,   'level' => 1, 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '3101', 'name_ar' => 'رأس المال',                 'name_en' => 'Capital',                    'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '3000', 'level' => 2, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '3102', 'name_ar' => 'أرباح محتجزة',              'name_en' => 'Retained Earnings',          'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '3000', 'level' => 2, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '3103', 'name_ar' => 'احتياطي قانوني',            'name_en' => 'Legal Reserve',              'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '3000', 'level' => 2, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '3104', 'name_ar' => 'احتياطي عام',               'name_en' => 'General Reserve',            'account_type' => 'equity',    'account_nature' => 'credit', 'parent_code' => '3000', 'level' => 2, 'is_header' => 0, 'is_system' => 0],

        // ══════════════════════════════════════════════════════════
        // 4. REVENUE
        // ══════════════════════════════════════════════════════════
        ['account_code' => '4000', 'name_ar' => 'الإيرادات',                 'name_en' => 'Revenue',                    'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => null,   'level' => 1, 'is_header' => 1, 'is_system' => 1],

        // Operating Revenue
        ['account_code' => '4100', 'name_ar' => 'إيرادات تشغيلية',           'name_en' => 'Operating Revenue',          'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4000', 'level' => 2, 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '4101', 'name_ar' => 'إيرادات عضوية',             'name_en' => 'Membership Revenue',         'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '4102', 'name_ar' => 'إيرادات اشتراكات',          'name_en' => 'Subscription Revenue',       'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '4103', 'name_ar' => 'إيرادات مبيعات',            'name_en' => 'Sales Revenue',              'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '4104', 'name_ar' => 'إيرادات غرامات',            'name_en' => 'Fine Revenue',               'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '4105', 'name_ar' => 'إيرادات خدمات',             'name_en' => 'Service Revenue',            'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '4106', 'name_ar' => 'إيرادات إيجارات',           'name_en' => 'Rental Revenue',             'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '4107', 'name_ar' => 'إيرادات حجوزات ومرافق',     'name_en' => 'Facility Revenue',           'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '4108', 'name_ar' => 'إيرادات أكاديميات',         'name_en' => 'Academy Revenue',            'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '4109', 'name_ar' => 'إيرادات كارنيهات',          'name_en' => 'Carnet Revenue',             'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],

        // Other Revenue
        ['account_code' => '4300', 'name_ar' => 'إيرادات أخرى',              'name_en' => 'Other Revenue',              'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4000', 'level' => 2, 'is_header' => 1, 'is_system' => 0],
        ['account_code' => '4301', 'name_ar' => 'فوائد بنكية دائنة',         'name_en' => 'Bank Interest Income',       'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4300', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '4302', 'name_ar' => 'إيرادات متنوعة',            'name_en' => 'Miscellaneous Income',       'account_type' => 'revenue',   'account_nature' => 'credit', 'parent_code' => '4300', 'level' => 3, 'is_header' => 0, 'is_system' => 0],

        // ══════════════════════════════════════════════════════════
        // 5. EXPENSES
        // ══════════════════════════════════════════════════════════
        ['account_code' => '5000', 'name_ar' => 'المصروفات',                 'name_en' => 'Expenses',                   'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => null,   'level' => 1, 'is_header' => 1, 'is_system' => 1],

        // Personnel Expenses
        ['account_code' => '5100', 'name_ar' => 'مصروفات الموظفين',          'name_en' => 'Personnel Expenses',         'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5000', 'level' => 2, 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '5101', 'name_ar' => 'مصروفات رواتب وأجور',       'name_en' => 'Salaries & Wages',           'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '5102', 'name_ar' => 'حصة صاحب العمل تأمينات',    'name_en' => 'Employer Insurance',         'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5100', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '5103', 'name_ar' => 'بدلات وحوافز',              'name_en' => 'Allowances & Bonuses',       'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5104', 'name_ar' => 'مصروفات تدريب',             'name_en' => 'Training Expenses',          'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5105', 'name_ar' => 'مكافأة نهاية خدمة',         'name_en' => 'End of Service Benefits',    'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5100', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5106', 'name_ar' => 'تكلفة البضاعة المباعة',     'name_en' => 'Cost of Goods Sold',         'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5000', 'level' => 2, 'is_header' => 0, 'is_system' => 1],

        // Operating Expenses
        ['account_code' => '5200', 'name_ar' => 'مصروفات تشغيلية',           'name_en' => 'Operating Expenses',         'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5000', 'level' => 2, 'is_header' => 1, 'is_system' => 1],
        ['account_code' => '5201', 'name_ar' => 'مصاريف بنكية',              'name_en' => 'Bank Charges',               'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '5202', 'name_ar' => 'مصروفات إيجار',             'name_en' => 'Rent Expense',               'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5203', 'name_ar' => 'مصروفات مرافق (كهرباء/مياه/غاز)', 'name_en' => 'Utilities',           'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5204', 'name_ar' => 'مصروفات صيانة',             'name_en' => 'Maintenance Expense',        'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5205', 'name_ar' => 'مصروفات إهلاك',             'name_en' => 'Depreciation Expense',       'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 1],
        ['account_code' => '5206', 'name_ar' => 'مصروفات نظافة',             'name_en' => 'Cleaning Expense',           'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5207', 'name_ar' => 'مصروفات أمن وحراسة',        'name_en' => 'Security Expense',           'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5208', 'name_ar' => 'مصروفات تأمين (ممتلكات)',   'name_en' => 'Property Insurance',         'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5209', 'name_ar' => 'مصروفات اتصالات وإنترنت',   'name_en' => 'Communications',             'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5210', 'name_ar' => 'مصروفات مطبوعات وقرطاسية',  'name_en' => 'Printing & Stationery',      'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5211', 'name_ar' => 'مصروفات ضيافة',             'name_en' => 'Hospitality',                'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5212', 'name_ar' => 'مصروفات نقل ومواصلات',      'name_en' => 'Transportation',             'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5213', 'name_ar' => 'مصروفات إعلان وتسويق',      'name_en' => 'Marketing & Advertising',    'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5214', 'name_ar' => 'مصروفات رسوم حكومية',       'name_en' => 'Government Fees',            'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5215', 'name_ar' => 'مصروفات استشارات قانونية',   'name_en' => 'Legal & Professional Fees',  'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
        ['account_code' => '5299', 'name_ar' => 'مصروفات متنوعة',            'name_en' => 'Miscellaneous Expenses',     'account_type' => 'expense',   'account_nature' => 'debit', 'parent_code' => '5200', 'level' => 3, 'is_header' => 0, 'is_system' => 0],
    ];

    // Build code => id map as we insert, so children can resolve parent_id
    $codeToId = [];
    $now = date('Y-m-d H:i:s');

    foreach ($accounts as $acct) {
        // Skip if already seeded
        $existing = $db->selectOne(
            "SELECT id FROM chart_of_accounts WHERE account_code = ?",
            [$acct['account_code']]
        );
        if ($existing) {
            $codeToId[$acct['account_code']] = (int) $existing['id'];
            continue;
        }

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

        unset($acct['parent_code']);

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

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