<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // Create Roles
        $superAdmin = Role::create(['name' => 'Super Admin']);
        $salesManager = Role::create(['name' => 'Sales Manager']);
        $salesAgent = Role::create(['name' => 'Sales Agent']);
        $customerSupport = Role::create(['name' => 'Customer Support']);
        $accountant = Role::create(['name' => 'Accountant']);
        $marketing = Role::create(['name' => 'Marketing Team']);

        $customerRole = Role::firstOrCreate(['name' => 'Customer']);

        // Create Demo Users
        $adminUser = User::factory()->create([
            'name' => 'Super Admin',
            'email' => 'superadmin@example.com',
            'password' => Hash::make('1234'),
        ]);
        $adminUser->assignRole($superAdmin);

        $managerUser = User::factory()->create([
            'name' => 'John Manager',
            'email' => 'manager@example.com',
            'password' => Hash::make('1234'),
        ]);
        $managerUser->assignRole($salesManager);

        $agentUser = User::factory()->create([
            'name' => 'Ali Agent',
            'email' => 'agent@example.com',
            'password' => Hash::make('1234'),
        ]);
        $agentUser->assignRole($salesAgent);

        $accountantUser = User::factory()->create([
            'name' => 'Sara Accountant',
            'email' => 'accountant@example.com',
            'password' => Hash::make('1234'),
        ]);
        $accountantUser->assignRole($accountant);
        
        $supportUser = User::factory()->create([
            'name' => 'Support Rep',
            'email' => 'support@example.com',
            'password' => Hash::make('1234'),
        ]);
        $supportUser->assignRole($customerSupport);

        // Create Test Customer User and Profile
        $testCustomerUser = User::firstOrCreate(
            ['email' => 'test_customer@example.com'],
            [
                'name' => 'Test Customer',
                'password' => Hash::make('1234'),
            ]
        );
        $testCustomerUser->assignRole($customerRole);

        \App\Models\Customer::firstOrCreate(
            ['email' => 'test_customer@example.com'],
            [
                'full_name' => 'Test Customer',
                'phone' => '03001234567',
                'customer_type' => 'Individual'
            ]
        );
    }
}
