<?php

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use App\Classes\ValueObjects\Constants as Constants;
use App\Models\User;

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

        // admin permissions
        $permissions = [
            ['name' => 'view document', 'guard_name' => 'api'],
            ['name' => 'add document', 'guard_name' => 'api'],
            ['name' => 'edit document', 'guard_name' => 'api'],
            ['name' => 'delete document', 'guard_name' => 'api'],

            ['name' => 'view standard_segment', 'guard_name' => 'api'],
            ['name' => 'add standard_segment', 'guard_name' => 'api'],
            ['name' => 'edit standard_segment', 'guard_name' => 'api'],
            ['name' => 'delete standard_segment', 'guard_name' => 'api'],

            ['name' => 'view standard_segment_constant', 'guard_name' => 'api'],
            ['name' => 'add standard_segment_constant', 'guard_name' => 'api'],
            ['name' => 'edit standard_segment_constant', 'guard_name' => 'api'],
            ['name' => 'delete standard_segment_constant', 'guard_name' => 'api'],

            ['name' => 'view segment', 'guard_name' => 'api'],
            ['name' => 'add segment', 'guard_name' => 'api'],
            ['name' => 'edit segment', 'guard_name' => 'api'],
            ['name' => 'delete segment', 'guard_name' => 'api'],

            ['name' => 'view segment_constant', 'guard_name' => 'api'],
            ['name' => 'add segment_constant', 'guard_name' => 'api'],
            ['name' => 'edit segment_constant', 'guard_name' => 'api'],
            ['name' => 'delete segment_constant', 'guard_name' => 'api'],

            ['name' => 'view company_bank', 'guard_name' => 'api'],
            ['name' => 'add company_bank', 'guard_name' => 'api'],
            ['name' => 'edit company_bank', 'guard_name' => 'api'],
            ['name' => 'delete company_bank', 'guard_name' => 'api'],

            ['name' => 'view currency', 'guard_name' => 'api'],
            ['name' => 'add currency', 'guard_name' => 'api'],
            ['name' => 'edit currency', 'guard_name' => 'api'],
            ['name' => 'delete currency', 'guard_name' => 'api'],

            ['name' => 'view currency_rate', 'guard_name' => 'api'],
            ['name' => 'add currency_rate', 'guard_name' => 'api'],
            ['name' => 'edit currency_rate', 'guard_name' => 'api'],
            ['name' => 'delete currency_rate', 'guard_name' => 'api'],

            ['name' => 'view booking', 'guard_name' => 'api'],
            ['name' => 'add booking', 'guard_name' => 'api'],
            ['name' => 'edit booking', 'guard_name' => 'api'],
            ['name' => 'delete booking', 'guard_name' => 'api'],

            ['name' => 'add milestone', 'guard_name' => 'api'],
            ['name' => 'add reward', 'guard_name' => 'api'],
            ['name' => 'edit milestone', 'guard_name' => 'api'],
            ['name' => 'delete milestone', 'guard_name' => 'api'],
            ['name' => 'delete reward', 'guard_name' => 'api'],

            ['name' => 'add voucher', 'guard_name' => 'api'],
            ['name' => 'list voucher campaigns', 'guard_name' => 'api'],

            ['name' => 'update bank_metadata', 'guard_name' => 'api'],
        ];

        foreach ($permissions as $permission){
            Permission::updateOrCreate($permission,$permission);
        }

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

        $shadow_admin_role = Role::updateOrCreate(
            ['name' => 'Shadow Admin'],
            [
                'name' => 'Shadow Admin',
                'guard_name' => 'api',
                'type' => 1,
            ]
        );

        $shadow_admin_role->givePermissionTo(Permission::all());

        //Assign permission by user role/type not id
        //$admin = User::find(1);
        //$admin->assignRole($shadow_admin_role);

        $shadow_admins = User::query()->whereIn('type',[Constants\RoleTypes::SHADOW_ADMIN])->get();
        foreach ($shadow_admins as $shadow_admin) {
            $shadow_admin->assignRole($shadow_admin_role);
        }
        ///////////////////////////////////////////////////////////////////////

        $ultimate_admin_role = Role::updateOrCreate(
            ['name' => 'Ultimate Admin'],
            [
                'name' => 'Ultimate Admin',
                'guard_name' => 'api',
                'type' => 1,
            ]
        );

        $ultimate_admin_role->givePermissionTo(Permission::all());

        $super_admins = User::query()->whereIn('type',[Constants\RoleTypes::SUPER_ADMIN])->get();
        foreach ($super_admins as $super_admin) {
            $super_admin->assignRole($ultimate_admin_role);
        }

        /* Disable normal admin role for now, will comeback later to this
        $admin_role = Role::updateOrCreate(
            ['name' => 'Admin'],
            [
                'name' => 'Admin',
                'guard_name' => 'api',
                'type' => 1,
            ]
        );

        //Normal admin limited permissions
        $admin_permissions = [
            $permissions[0]['name'],
            $permissions[1]['name'],
            $permissions[2]['name'],
            $permissions[3]['name'],
        ];

        $admin_role->givePermissionTo(Permission::query()->whereIn('name',$admin_permissions)->get());

        $admins = User::query()->whereIn('type',[Constants\RoleTypes::ADMIN])->get();
        foreach ($admins as $admin) {
            $admin->assignRole($admin_role);
        }
        */
        ///////////////////////////////////////////////////////////////////////
    }
}
