<?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()
    {
        $permissions = [
            'admin' => [
                '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',
                'add-server-license', 'edit-server-license', 'edit-csv-mapping', 'edit-print-form',
                'edit-user-interface', 'edit-global', 'edit-parameter', 'edit-onload',
                'edit-validate', 'edit-mapto', 'edit-mainvar', 'download-file-server-license',
                'list-history-patch', 'delete-server-license',
                'add-patch', 'edit-patch', 'delete-patch',
                'send-patch', 'resend-patch', 'delete-send-patch',
                'add-parameter', 'edit-parameter', 'delete-parameter',
                'add-exchange-rate', 'upload-exchange-rate', 'edit-exchange-rate', 'delete-exchange-rate',
                'upload-discharge-port'
            ],
            'developer' => [
                'add-server-license', 'edit-server-license', 'edit-csv-mapping', 'edit-print-form',
                'edit-user-interface', 'edit-global', 'edit-parameter', 'edit-onload',
                'edit-validate', 'edit-mapto', 'edit-mainvar', 'download-file-server-license',
                'add-patch', 'edit-patch', 'delete-patch',
                'add-parameter', 'edit-parameter', 'delete-parameter'
            ],
            'sales' => [
                'add-company', 'edit-company', 'delete-company',
                'add-company-type', 'edit-company-type', 'delete-company-type',
                'send-patch'
            ],
            'qa' => [
                'add-patch', 'edit-patch', 'delete-patch',
                'add-exchange-rate', 'upload-exchange-rate', 'edit-exchange-rate', 'delete-exchange-rate',
                'upload-discharge-port'
            ]
        ];

        // Create roles and assign permissions
        foreach ($permissions as $roleName => $perms) {
            $role = Role::create(['name' => $roleName]);

            foreach ($perms as $perm) {
                $permission = Permission::where('name', $perm)->first();
                if ($permission) {
                    $role->permissions()->attach($permission->id);
                }
            }
        }
    }
}
