<?php

namespace Database\Seeders;

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

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // Create Roles
        $adminRole = Role::updateOrCreate(
            ['name' => 'admin'],
            ['guard_name' => 'web']
        );

        Role::updateOrCreate(
            ['name' => 'staff'],
            ['guard_name' => 'web']
        );

        $userRole = Role::updateOrCreate(
            ['name' => 'user'],
            ['guard_name' => 'web']
        );

        // Create Admin
        $admin = User::updateOrCreate(
            ['email' => 'omigaadmin@gmail.com'],
            [
                'user_id'  => 'OM0000000001',
                'name'     => 'omiga',
                'pwd'      => Hash::make('Omiga@9911'),
                'pwd_hint' => 'Omiga@9911',
            ]
        );

        $admin->syncRoles($adminRole);

        // Assign roles to all users
        foreach (User::all() as $user) {

            if ($user->user_id === 'OM0000000001') {
                $user->syncRoles('admin');
            } else {
                $user->syncRoles('user');
            }
        }
    }
}
// <!-- <?php

// namespace Database\Seeders;

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

// class DatabaseSeeder extends Seeder
// {
//     /**
//      * Seed the application's database.
//      */
//     public function run(): void
//     {
//         // User::factory(10)->create();

//         Role::updateOrCreate(
//             ['name' => 'admin'],
//             ['guard_name' => 'web']
//         );

//         Role::updateOrCreate(
//             ['name' => 'user'],
//             ['guard_name' => 'web']
//         );

//         $users = User::all();

//         foreach ($users as $user) {

//             if ($user->user_id == 'OM0000000001') {

//                 $user->syncRoles('admin');

//             } else {

//                 $user->syncRoles('user');
//             }
//         }
//     }
// } -->
