<?php

namespace Database\Seeders;

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

class RolesAndPermissionsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Create roles
        Role::create(['name' => 'super admin']); //One to rule them all
        Role::create(['name' => 'admin']);      //2nd in command
        Role::create(['name' => 'manager']);    //IPRS staff
        Role::create(['name' => 'client']);     //IPRS Clients
        Role::create(['name' => 'user']);       //Low rank

        // Create permissions
        //Clients
        Permission::create(['name' => 'Manage Clients', 'permission_type'=>'Client', 'description'=>'All CRUD operations on a Client']);
        Permission::create(['name' => 'Create a Client', 'permission_type'=>'Client', 'description'=>'Create operation on a Client']);
        Permission::create(['name' => 'Update a Client', 'permission_type'=>'Client', 'description'=>'Update operation on a Client']);
        Permission::create(['name' => 'View any Client', 'permission_type'=>'Client', 'description'=>'View any Client on the system']);
        Permission::create(['name' => 'View own Client', 'permission_type'=>'Client', 'description'=>'View Client on the system']);
        //Contracts
        Permission::create(['name' => 'Manage Contracts', 'permission_type'=>'Contract', 'description'=>'All CRUD operations on a Contract']);
        Permission::create(['name' => 'Create a Contract', 'permission_type'=>'Contract', 'description'=>'Create operation on a Contract']);
        Permission::create(['name' => 'Edit a Contract', 'permission_type'=>'Contract', 'description'=>'Edit operation on a Contract']);
        Permission::create(['name' => 'View any Contract', 'permission_type'=>'Contract', 'description'=>'View any Client on the Contract']);
        Permission::create(['name' => 'View own Contract', 'permission_type'=>'Contract', 'description'=>'View Client on the Contract']);
        //Users
        Permission::create(['name' => 'Manage Users', 'permission_type'=>'User', 'description'=>'All CRUD operations on a Users']);
        Permission::create(['name' => 'Create a User', 'permission_type'=>'User', 'description'=>'Create operation on a User']);
        Permission::create(['name' => 'Update a User', 'permission_type'=>'Contract', 'description'=>'Update operation on a User']);
        Permission::create(['name' => 'View any User', 'permission_type'=>'Contract', 'description'=>'View any Client on the User']);
        //User groups
        Permission::create(['name' => 'Manage any User Groups', 'permission_type'=>'User Group', 'description'=>'All CRUD operations on all User Groups']);
        Permission::create(['name' => 'Manage own User Group', 'permission_type'=>'User Group', 'description'=>'CRUD operations on own User Group']);
        Permission::create(['name' => 'View User in a User Group', 'permission_type'=>'User Group', 'description'=>'View any user in own User Group']);
        Permission::create(['name' => 'Add User in a User Group', 'permission_type'=>'User Group', 'description'=>'Add users in own User Group']);
        Permission::create(['name' => 'Delete User in a User Group', 'permission_type'=>'User Group', 'description'=>'Delete any user in own User Group']);
        //System
        Permission::create(['name' => 'Administer System configuration', 'permission_type'=>'System', 'description'=>'Manager system configuration']);
        Permission::create(['name' => 'View System configuration', 'permission_type'=>'System', 'description'=>'View only system configuration']);
        //Reports
        Permission::create(['name' => 'View global statistics', 'permission_type'=>'Reports', 'description'=>'View system wide stats.']);
        Permission::create(['name' => 'View own statistics', 'permission_type'=>'Reports', 'description'=>'View own Account stats']);
        Permission::create(['name' => 'View Search statistics', 'permission_type'=>'Reports', 'description'=>'View Query/Search stats']);

        //Assign permissions to super admin
        Role::findByName('super admin')->givePermissionTo([
            'Manage Clients',
            'Manage Contracts',
            'Manage Users',
            'Manage any User Groups',
            'Administer System configuration',
            'View global statistics'
        ]);

        //Assign permissions to admin
        Role::findByName('admin')->givePermissionTo([
            'Manage Clients',
            'Manage Contracts',
            'Manage Users',
            'Manage any User Groups',
            'View global statistics'
        ]);

        //Assign permissions to client
        Role::findByName('manager')->givePermissionTo([
            'Manage any User Groups',
            'Create a Client',
            'Update a Client',
            'View any Client',
            'Create a Contract',
            'Edit a Contract',
            'View any Contract',
            'View Search statistics'
        ]);

        //Assign permissions to client
        Role::findByName('client')->givePermissionTo([
            'Manage own User Group',
            'View own statistics'
        ]);
    }
}
