<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Terminy\Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Exceptions\RoleDoesNotExist;
use Spatie\Permission\Exceptions\PermissionDoesNotExist;

/**
 * Class TerminyPouzivatelRoleOpravneniaDatabaseSeeder
 *
 * @package Seka\Portal\Modul\Terminy\Database\Seeders
 */
class TerminyPouzivatelRoleOpravneniaDatabaseSeeder extends Seeder
{
    /**
     * @return void
     */
    public function run(): void
    {
        collect([
            'terminy',
            'terminy.zobrazit',
            'terminy.udalosti.zobrazit',
            'terminy.udalosti.vytvorit',
            'terminy.udalosti.upravit',
            'terminy.udalosti.odstranit',
            'terminy.registracie.zobrazit',
            'terminy.registracie.vytvorit',
            'terminy.registracie.upravit',
            'terminy.registracie.odstranit',
        ])->each(function ($opr) {
            try {
                Permission::findByName($opr);
            } catch (PermissionDoesNotExist $exception) {
                Permission::create([
                    'name' => $opr,
                ]);
            }
        });

        collect([
            'technik',
            'absolvent',
            'spravca_pek',
        ])->each(function ($rola) {
            try {
                Role::findByName($rola);
            } catch (RoleDoesNotExist $exception) {
                Role::create([
                    'name' => $rola,
                ]);
            }
        });
    }
}
