<?php

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use App\Models\User;


/*
|--------------------------------------------------------------------------
| Console Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of your Closure based console
| commands. Each Closure is bound to a command instance allowing a
| simple approach to interacting with each command's IO methods.
|
*/

Artisan::command('assingManager {account}', function ($account) {
    $managerRole = Role::firstOrCreate([
        'name' => 'manager',
        'guard_name' => 'api'
    ]);

    $managers = User::where('account', $account)->get();

    foreach ($managers as $user) {
        $user->assignRole($managerRole);
    }
})->purpose('Display an inspiring quote');

Artisan::command('assingAdmin {account}', function ($account) {
    $adminRole = Role::firstOrCreate([
        'name' => 'admin',
        'guard_name' => 'api'
    ]);

    $managers = User::where('account', $account)->get();

    foreach ($managers as $user) {
        $user->assignRole($adminRole);
    }
})->purpose('Display an inspiring quote');

Artisan::command('assingKmEditor {account}', function ($account) {
    $kmEditorRole = Role::firstOrCreate([
        'name' => 'km editor',
        'guard_name' => 'api'
    ]);

    $editors = User::where('account', $account)->get();

    foreach ($editors as $user) {
        $user->assignRole($kmEditorRole);
    }
})->purpose('Display an inspiring quote');

Artisan::command('assingManager', function () {
    $managerRole = Role::firstOrCreate([
        'name' => 'manager',
        'guard_name' => 'api'
    ]);

    $managers = User::where('JobTitle', 'LIKE', "%經理%")->get();

    foreach ($managers as $user) {
        $user->assignRole($managerRole);
    }
})->purpose('Display an inspiring quote');

Artisan::command('assingSenior', function () {
    $seniorRole = Role::firstOrCreate([
        'name' => 'km senior',
        'guard_name' => 'api'
    ]);

    $deputyManagers = User::where('DeputyManager', 1)->get();

    foreach ($deputyManagers as $user) {
        $user->assignRole($seniorRole);
    }
})->purpose('Display an inspiring quote');

Artisan::command('assingUser', function () {
    $userRole = Role::firstOrCreate([
        'name' => 'user',
        'guard_name' => 'api'
    ]);
    $kmRole = Role::firstOrCreate([
        'name' => 'km user',
        'guard_name' => 'api'
    ]);

    $users = User::where('Disabled', 0)->get();

    foreach ($users as $user) {
        $user->assignRole($userRole);
        $user->assignRole($kmRole);
    }
})->purpose('Display an inspiring quote');

Artisan::command('initRBAC', function () {
    $superAdminRole = Role::firstOrCreate(['name' => 'superAdmin', 'guard_name' => 'api', 'alias' => '超級管理員']);
    $manageAllPermission = Permission::firstOrCreate(['name' => 'manage all', 'guard_name' => 'api']);
    $superAdminRole->givePermissionTo($manageAllPermission);

    $adminRole = Role::firstOrCreate(['name' => 'admin', 'guard_name' => 'api', 'alias' => '管理員']);
    $readAdminPermission = Permission::firstOrCreate(['name' => 'read admin', 'guard_name' => 'api']);
    $createAdminPermission = Permission::firstOrCreate(['name' => 'create admin', 'guard_name' => 'api']);
    $createPermission = Permission::firstOrCreate(['name' => 'create permission', 'guard_name' => 'api']);
    $readPermission = Permission::firstOrCreate(['name' => 'read permission', 'guard_name' => 'api']);
    $adminRole->givePermissionTo($readAdminPermission);
    $adminRole->givePermissionTo($createAdminPermission);
    $adminRole->givePermissionTo($createPermission);
    $adminRole->givePermissionTo($readPermission);

    $userRole = Role::firstOrCreate(['name' => 'user', 'guard_name' => 'api', 'alias' => '一般用戶']);
    $readBasePermission = Permission::firstOrCreate(['name' => 'read base', 'guard_name' => 'api']);
    $userRole->givePermissionTo($readBasePermission);

    $managerRole = Role::firstOrCreate(['name' => 'manager', 'guard_name' => 'api', 'alias' => '主管']);
    $managerRole->givePermissionTo($createPermission);
    $managerRole->givePermissionTo($readPermission);

    $kmUserRole = Role::firstOrCreate(['name' => 'km user', 'guard_name' => 'api', 'alias' => 'KM用戶']);
    $readKMPermission = Permission::firstOrCreate(['name' => 'read KM', 'guard_name' => 'api']);
    $kmUserRole->givePermissionTo($readKMPermission);

    $kmSeniorRole = Role::firstOrCreate(['name' => 'km senior', 'guard_name' => 'api', 'alias' => 'KM高階用戶']);
    $readKMSeniorPermission = Permission::firstOrCreate(['name' => 'read KM_senior', 'guard_name' => 'api']);
    $kmSeniorRole->givePermissionTo($readKMSeniorPermission);
    $kmSeniorRole->givePermissionTo($readKMPermission);

    $kmEditorRole = Role::firstOrCreate(['name' => 'km editor', 'guard_name' => 'api', 'alias' => 'KM編輯人員']);
    $createKMSeniorPermission = Permission::firstOrCreate(['name' => 'create KM_senior', 'guard_name' => 'api']);
    $createKMPermission = Permission::firstOrCreate(['name' => 'create KM', 'guard_name' => 'api']);
    $kmEditorRole->givePermissionTo($createKMSeniorPermission);
    $kmEditorRole->givePermissionTo($createKMPermission);
    $kmEditorRole->givePermissionTo($readKMSeniorPermission);
    $kmEditorRole->givePermissionTo($readKMPermission);
})->purpose('Create roles and permissions with aliases');

Artisan::command('setupRBACAndAssignRoles {adminAccount?}', function ($adminAccount = null) {
    $this->call('initRBAC');
    $this->call('assingUser');
    $this->call('assingSenior');
    $this->call('assingManager');

    if (!is_null($adminAccount)) {
        $this->call('assingAdmin', [
            'account' => $adminAccount
        ]);
    }

})->purpose('Setup RBAC and assign roles');
