<?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\Admin;

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

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

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

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

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

        $shadow_admin_role = Role::create([
            'name' => 'Shadow Admin', 
            'guard_name' => 'admin',
            'status' => 1
        ]);

        $shadow_admin_role->givePermissionTo(Permission::where('guard_name', 'admin')->get());

        $admin = Admin::find(1);
        $admin->assignRole($shadow_admin_role);

        ///////////////////////////////////////////////////////////////////////

        $ultimate_admin_role = Role::create([
            'name' => 'Ultimate Admin', 
            'guard_name' => 'admin',
            'status' => 1
        ]);

        $ultimate_admin_role->givePermissionTo(Permission::where('guard_name', 'admin')->get());

        $admin = Admin::find(2);
        $admin->assignRole($ultimate_admin_role);
    }
}
