<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        User::updateOrCreate(
            ['email' => 'admin@finance.test'],
            [
                'name' => 'HR Admin',
                'phone' => '+2348000000001',
                'department' => 'Finance and Accounts',
                'position' => 'Finance Administrator',
                'role' => 'admin',
                'status' => 'active',
                'password' => Hash::make('Password@123'),
            ]
        );

        for ($i = 1; $i <= 3; $i++) {
            User::updateOrCreate(
                ['email' => "user{$i}@finance.test"],
                [
                    'name' => "Finance User {$i}",
                    'phone' => '+234800000000' . ($i + 1),
                    'department' => ['Operations', 'Procurement', 'Administration'][$i - 1],
                    'position' => 'Staff',
                    'role' => 'user',
                    'status' => 'active',
                    'password' => Hash::make('Password@123'),
                ]
            );
        }

        $accounts = [
            ['1000', 'Cash at Bank', 'asset', 'bank', 2500000, true, true],
            ['1100', 'Accounts Receivable', 'asset', 'receivable', 0, false, true],
            ['1200', 'Staff Advances', 'asset', 'advance', 0, false, true],
            ['2000', 'Accounts Payable', 'liability', 'payable', 0, false, true],
            ['2100', 'Tax Payable', 'liability', 'tax', 0, false, true],
            ['3000', 'Owner Equity', 'equity', 'capital', 2500000, false, true],
            ['4000', 'Service Revenue', 'income', 'sales', 0, false, true],
            ['4100', 'Other Income', 'income', 'other', 0, false, true],
            ['5000', 'Operating Expenses', 'expense', 'operations', 0, false, true],
            ['5100', 'Payroll Expense', 'expense', 'payroll', 0, false, true],
            ['5200', 'Transport Expense', 'expense', 'transport', 0, false, true],
            ['5300', 'Office Supplies', 'expense', 'office', 0, false, true],
        ];

        foreach ($accounts as [$code, $name, $type, $subtype, $balance, $cash, $system]) {
            DB::table('chart_of_accounts')->updateOrInsert(
                ['code' => $code],
                [
                    'name' => $name,
                    'type' => $type,
                    'subtype' => $subtype,
                    'opening_balance' => $balance,
                    'current_balance' => $balance,
                    'is_cash_account' => $cash,
                    'is_system' => $system,
                    'status' => 'active',
                    'created_at' => now(),
                    'updated_at' => now(),
                ]
            );
        }

        $cashAccountId = DB::table('chart_of_accounts')->where('code', '1000')->value('id');
        DB::table('bank_accounts')->updateOrInsert(
            ['bank_name' => 'Demo Bank', 'account_name' => 'Movex Finance Main Account'],
            [
                'chart_of_account_id' => $cashAccountId,
                'account_number' => '0000000001',
                'currency' => 'NGN',
                'opening_balance' => 2500000,
                'current_balance' => 2500000,
                'status' => 'active',
                'created_at' => now(),
                'updated_at' => now(),
            ]
        );

        $contacts = [
            ['supplier', 'Lagos Office Supplies Ltd', 'accounts@lagosoffice.test', '+2348011111111'],
            ['customer', 'Demo Corporate Client', 'finance@client.test', '+2348022222222'],
            ['supplier', 'Reliable Transport Vendor', 'billing@transport.test', '+2348033333333'],
        ];

        foreach ($contacts as [$type, $name, $email, $phone]) {
            DB::table('finance_contacts')->updateOrInsert(
                ['email' => $email],
                [
                    'type' => $type,
                    'name' => $name,
                    'phone' => $phone,
                    'tax_number' => null,
                    'address' => 'Nigeria',
                    'status' => 'active',
                    'created_at' => now(),
                    'updated_at' => now(),
                ]
            );
        }
    }
}
