<?php

namespace Database\Seeders;

use App\Lib\Constants;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Database\Seeder;

class RoleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $role = Role::updateOrCreate([
            'name' => 'admin',
            'layout' => 'admin'
        ]);

        foreach(Permission::get() as $permission) {
            $permission->assignRole($role);
        }

        $role = Role::updateOrCreate([
            'name' => 'guest',
            'layout' => 'guest'
        ]);

        $guestPermissions = [
            'user.show',
            'user.create',
            'profile.read',
            'profile.update',
        ];

        $guestFullPermissions = [
            'sertifikat-vaksin.*',
        ];

        $arrAction = Constants::getMenuActions($guestFullPermissions);
        $guestPermissions = array_merge($guestPermissions, $arrAction);

        $permissions = Permission::query()
        ->whereIn('name', $guestPermissions)
        ->get();

        foreach($permissions as $permission) {
            $permission->assignRole($role);
        }
    }
}
