<?php

namespace Database\Seeders;

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


class RoleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Define permissions for each role
        $adminPermissions = [
            'add-header', 'edit-header', 'delete-header', 'send-customs', 'export-to-spn',
            'cancel-header', 'cancel-detail', 'merge-vessel', 'add-detail', 'edit-detail',
            'add-item', 'edit-item', 'download-file', 'change-flight',
            'add-user', 'edit-user', 'delete-user',
            'add-role', 'edit-role', 'delete-role',
            'add-group', 'edit-group', 'delete-group',
            'add-company', 'edit-company', 'delete-company',
            'add-company-type', 'edit-company-type', 'delete-company-type',
        ];

        $managerPermissions = [
            'add-header', 'edit-header', 'delete-header', 'send-customs',
            'cancel-header', 'cancel-detail', 'merge-vessel', 'add-detail', 'edit-detail',
            'add-item', 'edit-item', 'download-file', 'change-flight',
            'add-user', 'edit-user',
            'add-group', 'edit-group',
            'add-company', 'edit-company',
            'add-company-type', 'edit-company-type',
        ];

        $userPermissions = [
            'add-header', 'edit-header', 'send-customs',
            'cancel-header', 'cancel-detail', 'add-detail', 'edit-detail',
            'add-item', 'edit-item', 'download-file',
        ];

        // Create roles
        $adminRole = Role::create(['name' => 'Admin', 'description' => 'Administrator']);
        $managerRole = Role::create(['name' => 'Manager', 'description' => 'Manager']);
        $userRole = Role::create(['name' => 'User', 'description' => 'User']);

        // Assign permissions to roles
        $this->assignPermissions($adminRole, $adminPermissions);
        $this->assignPermissions($managerRole, $managerPermissions);
        $this->assignPermissions($userRole, $userPermissions);
    }

    private function assignPermissions($role, $permissions)
    {
        foreach ($permissions as $permissionName) {
            $permission = Permission::where('name', $permissionName)->first();
            if ($permission) {
                $role->permissions()->attach($permission);
            }
        }
    }
}
