<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Support\Facades\DB;

use App\Models\User;

class UserPermissionsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Reset cached roles and permissions
        app()['cache']->forget('spatie.permission.cache');

        // user permissions
        Permission::create(['name' => 'view entity', 'guard_name' => 'user']);
        Permission::create(['name' => 'add entity', 'guard_name' => 'user']);
        Permission::create(['name' => 'edit entity', 'guard_name' => 'user']);
        Permission::create(['name' => 'delete entity', 'guard_name' => 'user']);

        Permission::create(['name' => 'view contract_template', 'guard_name' => 'user']);
        Permission::create(['name' => 'add contract_template', 'guard_name' => 'user']);
        Permission::create(['name' => 'edit contract_template', 'guard_name' => 'user']);
        Permission::create(['name' => 'delete contract_template', 'guard_name' => 'user']);

        Permission::create(['name' => 'view contract_template_obligation', 'guard_name' => 'user']);
        Permission::create(['name' => 'add contract_template_obligation', 'guard_name' => 'user']);
        Permission::create(['name' => 'edit contract_template_obligation', 'guard_name' => 'user']);
        Permission::create(['name' => 'delete contract_template_obligation', 'guard_name' => 'user']);

        Permission::create(['name' => 'view contract_template_obligation_dependency', 'guard_name' => 'user']);
        Permission::create(['name' => 'add contract_template_obligation_dependency', 'guard_name' => 'user']);
        Permission::create(['name' => 'edit contract_template_obligation_dependency', 'guard_name' => 'user']);
        Permission::create(['name' => 'delete contract_template_obligation_dependency', 'guard_name' => 'user']);

        Permission::create(['name' => 'view contract', 'guard_name' => 'user']);
        Permission::create(['name' => 'add contract', 'guard_name' => 'user']);
        Permission::create(['name' => 'edit contract', 'guard_name' => 'user']);
        Permission::create(['name' => 'delete contract', 'guard_name' => 'user']);

        Permission::create(['name' => 'view contract_entity', 'guard_name' => 'user']);
        Permission::create(['name' => 'add contract_entity', 'guard_name' => 'user']);
        Permission::create(['name' => 'edit contract_entity', 'guard_name' => 'user']);
        Permission::create(['name' => 'delete contract_entity', 'guard_name' => 'user']);

        Permission::create(['name' => 'view contract_obligation', 'guard_name' => 'user']);
        Permission::create(['name' => 'add contract_obligation', 'guard_name' => 'user']);
        Permission::create(['name' => 'edit contract_obligation', 'guard_name' => 'user']);
        Permission::create(['name' => 'delete contract_obligation', 'guard_name' => 'user']);

        $normal_user_role = Role::create([
            'name' => 'Normal User', 
            'guard_name' => 'user',
            'status' => 1
        ]);

        $normal_user_role->givePermissionTo(Permission::where('guard_name', 'user')->get());

        $user = User::find(1);
        $user->assignRole($normal_user_role);

        $user = User::find(2);
        $user->assignRole($normal_user_role);

        $user = User::find(3);
        $user->assignRole($normal_user_role);

        ///////////////////////////////////////////////////////////////////////
    }
}
