<?php

namespace Database\Seeders;

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

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        // Call the permission seeder
        $this->call(PermissionSeeder::class);
        $this->call(CustomerSeeder::class);
        $this->call(ProductSeeder::class);

        // Create super admin user
        User::firstOrCreate(
            ['email' => 'superadmin@example.com'],
            [
                'name' => 'Super Admin',
                'password' => Hash::make('password'),
                'role' => 'super_admin',
                'total_balance' => 0,
            ]
        );

        // Create admin user
        User::firstOrCreate(
            ['email' => 'admin@example.com'],
            [
                'name' => 'Admin User',
                'password' => Hash::make('password'),
                'role' => 'admin',
                'total_balance' => 0,
            ]
        );

        // Create test user
        User::firstOrCreate(
            ['email' => 'user@example.com'],
            [
                'name' => 'Test User',
                'password' => Hash::make('password'),
                'role' => 'user',
                'total_balance' => 0,
        ]);

        // Create segments
        $segments = [
            'Sourcing',
            'Stock Maintain',
            'Marketing',
            'Sales',
            'Packaging & Delivery',
            'Accounting & Finance',
            'Technology',
        ];

        foreach ($segments as $segment) {
            Segment::firstOrCreate(['name' => $segment]);
        }

        // Create system expense types
        $expenseTypes = [
            'Cost of Sales' => 'Cost of Sales',
            'Delivery' => 'Delivery',
            'COD' => 'COD',
            'Salary' => 'Salary',
            'Internet' => 'Internet',
            'Transport' => 'Transport',
            'Rent' => 'Rent',
            'Refreshment' => 'Refreshment',
            'Office Stationery' => 'Office Stationery',
            'Writing Fee' => 'Writing Fee',
            'Equipment' => 'Equipment',
        ];

        foreach ($expenseTypes as $name => $category) {
            ExpenseType::firstOrCreate(
                ['name' => $name],
                [
                    'category' => $category,
                    'is_system' => true,
                ]
            );
        }
    }
}
