<?php

namespace Database\Seeders;

use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // Seed roles and permissions first
        $this->call([
            RolePermissionSeeder::class,
            ApiKeySeeder::class,
        ]);

        // Create a test admin user
        User::factory()->create([
            'name' => 'Admin User',
            'email' => 'admin@example.com',
            'role_type' => 3, // 3=admin
            'status' => 1, // 1=ACTIVE
        ]);

        // Create a test landlord user
        User::factory()->create([
            'name' => 'Landlord User',
            'email' => 'landlord@example.com',
            'role_type' => 1, // 1=landlord
            'status' => 1, // 1=ACTIVE
        ]);

        // Create a test tenant user
        User::factory()->create([
            'name' => 'Tenant User',
            'email' => 'tenant@example.com',
            'role_type' => 2, // 2=tenant
            'status' => 1, // 1=ACTIVE
        ]);

        // Assign roles to all users
        $this->assignRolesToUsers();
    }

    /**
     * Assign roles to all users based on their role_type
     */
    private function assignRolesToUsers(): void
    {
        $users = User::all();
        
        foreach ($users as $user) {
            // Remove existing roles first
            $user->syncRoles([]);
            
            // Assign role based on role_type
            switch ($user->role_type) {
                case 1: // landlord
                    $user->assignRole('landlord');
                    break;
                case 2: // tenant
                    $user->assignRole('tenant');
                    break;
                case 3: // admin
                    $user->assignRole('admin');
                    break;
            }
        }
    }
}
