<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $this->call(HiddenAdminSeeder::class);

        $now = Carbon::now();

        // 1. Seed Payment Methods
        $paymentMethods = ['Cash', 'Cheque', 'Bank Transfer', 'Credit/Debit Card'];
        foreach ($paymentMethods as $method) {
            DB::table('payment_methods')->updateOrInsert(
                ['name' => $method],
                ['created_at' => $now, 'updated_at' => $now]
            );
        }

        // 2. Seed Payment Types (Inflow / Outflow)
        $paymentTypes = [
            ['name' => 'Client Payment', 'direction' => 'in'],
            ['name' => 'Advance Payment', 'direction' => 'in'],
            ['name' => 'General Inflow', 'direction' => 'in'],
            ['name' => 'Client Refund', 'direction' => 'out'],
            ['name' => 'Expense Payment', 'direction' => 'out'],
            ['name' => 'General Outflow', 'direction' => 'out'],
        ];
        foreach ($paymentTypes as $type) {
            DB::table('payment_types')->updateOrInsert(
                ['name' => $type['name'], 'direction' => $type['direction']],
                ['created_at' => $now, 'updated_at' => $now]
            );
        }

        // 3. Seed General Business Expense Categories and Sub-categories
        $categories = [
            'Office Expenses' => ['Rent & Rates', 'Stationery & Printing', 'Internet & Phone', 'Software & Subscriptions'],
            'Professional Fees' => ['Legal & Compliance', 'Accounting & Bookkeeping', 'Consulting'],
            'Travel & Vehicle' => ['Fuel', 'Vehicle Maintenance', 'Public Transport', 'Hotel & Lodging'],
            'Salaries & Wages' => ['Direct Payroll', 'Contractor Fees', 'Staff Benefits'],
            'Marketing & Sales' => ['Advertising', 'Promo Materials', 'SEO & Website'],
            'Finance & Insurance' => ['Bank Fees', 'Business Insurance', 'Loan Interest'],
            'General Overhead' => ['Heat & Light', 'Cleaning & Sanitation', 'Repairs & Maintenance'],
        ];

        foreach ($categories as $parentName => $subs) {
            // Insert parent
            DB::table('expense_categories')->updateOrInsert(
                ['name' => $parentName, 'parent_id' => null],
                ['created_at' => $now, 'updated_at' => $now]
            );

            // Fetch parent ID
            $parentRecord = DB::table('expense_categories')
                ->where('name', $parentName)
                ->whereNull('parent_id')
                ->first();

            if ($parentRecord) {
                foreach ($subs as $subName) {
                    DB::table('expense_categories')->updateOrInsert(
                        ['name' => $subName, 'parent_id' => $parentRecord->id],
                        ['created_at' => $now, 'updated_at' => $now]
                    );
                }
            }
        }
    }
}
