<?php

declare(strict_types=1);

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

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

/**
 * Class TerminyPouzivatelRoleOpravneniaSeeder
 *
 * @package Seka\Portal\Modul\Terminy\Database\Seeders
 */
class TerminyPouzivatelRoleOpravneniaSeeder extends Seeder
{
    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,
                ]);
            }
        });
    }
}
