<?php

namespace Database\Seeders;

use App\Models\ChartOfAccount;
use Illuminate\Database\Seeder;

class ChartOfAccountSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $accounts = [
            // Assets (1000-1999)
            ['code' => '1000', 'name' => 'Assets', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => true, 'parent_code' => null],
            ['code' => '1010', 'name' => 'Cash on Hand', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => true, 'parent_code' => '1000'],
            ['code' => '1020', 'name' => 'Petty Cash', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => true, 'parent_code' => '1000'],
            ['code' => '1100', 'name' => 'Bank Accounts', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => true, 'parent_code' => '1000'],
            ['code' => '1110', 'name' => 'Checking Account', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => false, 'parent_code' => '1100'],
            ['code' => '1120', 'name' => 'Savings Account', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => false, 'parent_code' => '1100'],
            ['code' => '1200', 'name' => 'Accounts Receivable', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => true, 'parent_code' => '1000'],
            ['code' => '1300', 'name' => 'Inventory', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => true, 'parent_code' => '1000'],
            ['code' => '1400', 'name' => 'Prepaid Expenses', 'type' => 'asset', 'category' => 'current_asset', 'is_system' => false, 'parent_code' => '1000'],
            ['code' => '1500', 'name' => 'Fixed Assets', 'type' => 'asset', 'category' => 'fixed_asset', 'is_system' => true, 'parent_code' => '1000'],
            ['code' => '1510', 'name' => 'Office Equipment', 'type' => 'asset', 'category' => 'fixed_asset', 'is_system' => false, 'parent_code' => '1500'],
            ['code' => '1520', 'name' => 'Computer Equipment', 'type' => 'asset', 'category' => 'fixed_asset', 'is_system' => false, 'parent_code' => '1500'],
            ['code' => '1530', 'name' => 'Furniture & Fixtures', 'type' => 'asset', 'category' => 'fixed_asset', 'is_system' => false, 'parent_code' => '1500'],
            ['code' => '1540', 'name' => 'Vehicles', 'type' => 'asset', 'category' => 'fixed_asset', 'is_system' => false, 'parent_code' => '1500'],
            ['code' => '1600', 'name' => 'Accumulated Depreciation', 'type' => 'asset', 'category' => 'fixed_asset', 'is_system' => true, 'parent_code' => '1000'],

            // Liabilities (2000-2999)
            ['code' => '2000', 'name' => 'Liabilities', 'type' => 'liability', 'category' => 'current_liability', 'is_system' => true, 'parent_code' => null],
            ['code' => '2010', 'name' => 'Accounts Payable', 'type' => 'liability', 'category' => 'current_liability', 'is_system' => true, 'parent_code' => '2000'],
            ['code' => '2100', 'name' => 'Credit Cards Payable', 'type' => 'liability', 'category' => 'current_liability', 'is_system' => false, 'parent_code' => '2000'],
            ['code' => '2200', 'name' => 'Accrued Expenses', 'type' => 'liability', 'category' => 'current_liability', 'is_system' => false, 'parent_code' => '2000'],
            ['code' => '2300', 'name' => 'Sales Tax Payable', 'type' => 'liability', 'category' => 'current_liability', 'is_system' => true, 'parent_code' => '2000'],
            ['code' => '2400', 'name' => 'Payroll Liabilities', 'type' => 'liability', 'category' => 'current_liability', 'is_system' => false, 'parent_code' => '2000'],
            ['code' => '2500', 'name' => 'Long-term Debt', 'type' => 'liability', 'category' => 'long_term_liability', 'is_system' => false, 'parent_code' => '2000'],

            // Equity (3000-3999)
            ['code' => '3000', 'name' => 'Equity', 'type' => 'equity', 'category' => 'equity', 'is_system' => true, 'parent_code' => null],
            ['code' => '3010', 'name' => 'Owner\'s Equity', 'type' => 'equity', 'category' => 'equity', 'is_system' => true, 'parent_code' => '3000'],
            ['code' => '3020', 'name' => 'Retained Earnings', 'type' => 'equity', 'category' => 'retained_earnings', 'is_system' => true, 'parent_code' => '3000'],
            ['code' => '3030', 'name' => 'Owner\'s Draws', 'type' => 'equity', 'category' => 'equity', 'is_system' => false, 'parent_code' => '3000'],

            // Revenue (4000-4999)
            ['code' => '4000', 'name' => 'Revenue', 'type' => 'revenue', 'category' => 'operating_revenue', 'is_system' => true, 'parent_code' => null],
            ['code' => '4010', 'name' => 'Sales Revenue', 'type' => 'revenue', 'category' => 'operating_revenue', 'is_system' => true, 'parent_code' => '4000'],
            ['code' => '4020', 'name' => 'Service Revenue', 'type' => 'revenue', 'category' => 'operating_revenue', 'is_system' => false, 'parent_code' => '4000'],
            ['code' => '4030', 'name' => 'Consulting Revenue', 'type' => 'revenue', 'category' => 'operating_revenue', 'is_system' => false, 'parent_code' => '4000'],
            ['code' => '4100', 'name' => 'Other Income', 'type' => 'revenue', 'category' => 'other_revenue', 'is_system' => false, 'parent_code' => '4000'],
            ['code' => '4110', 'name' => 'Interest Income', 'type' => 'revenue', 'category' => 'other_revenue', 'is_system' => false, 'parent_code' => '4100'],
            ['code' => '4120', 'name' => 'Dividend Income', 'type' => 'revenue', 'category' => 'other_revenue', 'is_system' => false, 'parent_code' => '4100'],

            // Expenses (5000-5999)
            ['code' => '5000', 'name' => 'Expenses', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => true, 'parent_code' => null],
            ['code' => '5010', 'name' => 'Cost of Goods Sold', 'type' => 'expense', 'category' => 'cost_of_goods_sold', 'is_system' => true, 'parent_code' => '5000'],
            ['code' => '5100', 'name' => 'Office Supplies', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5110', 'name' => 'Travel Expenses', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5120', 'name' => 'Utilities', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5130', 'name' => 'Software & Subscriptions', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5140', 'name' => 'Marketing & Advertising', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5150', 'name' => 'Professional Services', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5160', 'name' => 'Equipment Expense', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5170', 'name' => 'Insurance', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5180', 'name' => 'Rent Expense', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5190', 'name' => 'Maintenance & Repairs', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5200', 'name' => 'Telecommunications', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5210', 'name' => 'Training & Education', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5220', 'name' => 'Meals & Entertainment', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5300', 'name' => 'Payroll Expenses', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => true, 'parent_code' => '5000'],
            ['code' => '5310', 'name' => 'Salaries & Wages', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5300'],
            ['code' => '5320', 'name' => 'Payroll Taxes', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5300'],
            ['code' => '5330', 'name' => 'Employee Benefits', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5300'],
            ['code' => '5400', 'name' => 'Depreciation Expense', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => true, 'parent_code' => '5000'],
            ['code' => '5500', 'name' => 'Bank Fees', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5600', 'name' => 'Interest Expense', 'type' => 'expense', 'category' => 'other_expense', 'is_system' => false, 'parent_code' => '5000'],
            ['code' => '5700', 'name' => 'Tax Expense', 'type' => 'expense', 'category' => 'other_expense', 'is_system' => true, 'parent_code' => '5000'],
            ['code' => '5900', 'name' => 'Miscellaneous Expense', 'type' => 'expense', 'category' => 'operating_expense', 'is_system' => false, 'parent_code' => '5000'],
        ];

        foreach ($accounts as $index => $accountData) {
            $parentCode = $accountData['parent_code'] ?? null;
            unset($accountData['parent_code']);

            $parentId = null;
            if ($parentCode) {
                $parent = ChartOfAccount::where('code', $parentCode)->first();
                if ($parent) {
                    $parentId = $parent->id;
                }
            }

            ChartOfAccount::updateOrCreate(
                ['code' => $accountData['code']],
                array_merge($accountData, [
                    'parent_id' => $parentId,
                    'opening_balance' => 0,
                    'current_balance' => 0,
                    'is_active' => true,
                    'sort_order' => $index,
                ])
            );
        }
    }
}
