<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\System\Roles\Role;
use Illuminate\Support\Facades\DB;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->seedStarterKit();
    }

    /**
     * Starter kit data
     */
    public function seedStarterKit(): void
    {
        $this->seedRoles();
        
        collect([
            [
                'first_name' => 'Nurmuhammet',
                'last_name' => 'Allanov',
                'email' => 'nurmuhammet@mail.com',
                'password' => bcrypt('payload10'),
            ],
        ])->each(function ($data) {
            $user = User::create($data);

            $user->assignRole('admin');
        });
    }

    public function seedRoles(): void
    {
        $roles = [
            [
                'id' => 1,
                'name' => 'admin',
                'guard_name' => 'web',
            ],
            [
                'id' => 2,
                'name' => 'manager',
                'guard_name' => 'web',
            ],
            [
                'id' => 3,
                'name' => 'user',
                'guard_name' => 'web',
            ],
            [
                'id' => 4,
                'name' => 'vendor',
                'guard_name' => 'web',
            ],
            [
                'id' => 5,
                'name' => 'viewer',
                'guard_name' => 'web',
            ],
        ];

        foreach ($roles as $role) {
            Role::forceCreate($role);
        }

        DB::statement("
            SELECT setval('roles_id_seq', (SELECT MAX(id) from roles))
        ");
    }
}
