<?php

namespace Database\Seeders;

use App\Domain\Financial\Models\FinancialAccount;
use App\Domain\Shared\Models\Academy;
use Illuminate\Database\Seeder;

class FinancialAccountsSeeder extends Seeder
{
    public function run(): void
    {
        $academy = Academy::first();

        if (!$academy) {
            $this->command->warn('No academy exists yet — skipping financial accounts seeder.');
            return;
        }

        $accounts = [
            // Assets
            ['code' => '1000', 'name' => 'Cash', 'name_ar' => 'النقدية', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => true],
            ['code' => '1010', 'name' => 'Bank Account', 'name_ar' => 'الحساب البنكي', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => true],
            ['code' => '1100', 'name' => 'Accounts Receivable', 'name_ar' => 'المدينون', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => true],
            ['code' => '1200', 'name' => 'Wallets Receivable', 'name_ar' => 'أرصدة المحافظ', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => true],

            // Revenue
            ['code' => '4000', 'name' => 'Training Revenue', 'name_ar' => 'إيرادات التدريب', 'type' => 'revenue', 'category' => 'operating', 'is_system' => true],
            ['code' => '4010', 'name' => 'Registration Fees', 'name_ar' => 'رسوم التسجيل', 'type' => 'revenue', 'category' => 'operating', 'is_system' => true],
            ['code' => '4020', 'name' => 'Equipment Sales', 'name_ar' => 'مبيعات المعدات', 'type' => 'revenue', 'category' => 'operating', 'is_system' => true],
            ['code' => '4030', 'name' => 'Facility Rental', 'name_ar' => 'إيجار الملاعب', 'type' => 'revenue', 'category' => 'operating', 'is_system' => true],
            ['code' => '4040', 'name' => 'Private Sessions', 'name_ar' => 'الحصص الخاصة', 'type' => 'revenue', 'category' => 'operating', 'is_system' => true],
            ['code' => '4050', 'name' => 'Tournament Fees', 'name_ar' => 'رسوم البطولات', 'type' => 'revenue', 'category' => 'operating', 'is_system' => true],
            ['code' => '4060', 'name' => 'External Revenue', 'name_ar' => 'إيرادات خارجية', 'type' => 'revenue', 'category' => 'operating', 'is_system' => true],

            // Expenses
            ['code' => '5000', 'name' => 'Trainer Salaries', 'name_ar' => 'رواتب المدربين', 'type' => 'expense', 'category' => 'operating', 'is_system' => true],
            ['code' => '5010', 'name' => 'Facility Rent', 'name_ar' => 'إيجار المنشآت', 'type' => 'expense', 'category' => 'operating', 'is_system' => true],
            ['code' => '5020', 'name' => 'Equipment Purchases', 'name_ar' => 'مشتريات المعدات', 'type' => 'expense', 'category' => 'operating', 'is_system' => true],
            ['code' => '5030', 'name' => 'Utilities', 'name_ar' => 'المرافق', 'type' => 'expense', 'category' => 'operating', 'is_system' => true],
            ['code' => '5040', 'name' => 'Marketing', 'name_ar' => 'التسويق', 'type' => 'expense', 'category' => 'operating', 'is_system' => true],
            ['code' => '5050', 'name' => 'Miscellaneous Expenses', 'name_ar' => 'مصروفات نثرية', 'type' => 'expense', 'category' => 'operating', 'is_system' => true],

            // Liability
            ['code' => '2000', 'name' => 'Accounts Payable', 'name_ar' => 'الدائنون', 'type' => 'liability', 'category' => 'current_liability', 'is_system' => true],
            ['code' => '2010', 'name' => 'Unearned Revenue', 'name_ar' => 'إيرادات مقدمة', 'type' => 'liability', 'category' => 'current_liability', 'is_system' => true],
            ['code' => '2020', 'name' => 'Refunds Payable', 'name_ar' => 'مستردات مستحقة', 'type' => 'liability', 'category' => 'current_liability', 'is_system' => true],
            // Tax collected from a customer is held for the Authority, not
            // earned — it credits a liability, never a revenue account.
            ['code' => '2030', 'name' => 'VAT Payable', 'name_ar' => 'ضريبة القيمة المضافة المستحقة', 'type' => 'liability', 'category' => 'current_liability', 'is_system' => true],
            ['code' => '2040', 'name' => 'Withholding Tax Payable', 'name_ar' => 'ضرائب خصم وإضافة مستحقة', 'type' => 'liability', 'category' => 'current_liability', 'is_system' => true],
        ];

        foreach ($accounts as $account) {
            FinancialAccount::firstOrCreate(
                ['academy_id' => $academy->id, 'code' => $account['code']],
                $account + ['academy_id' => $academy->id]
            );
        }
    }
}
