<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Gate;

use App\Models\User;
use App\Models\LesenPenjaja;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        //
        Gate::define('hapus-sendiri', function (User $user, LesenPenjaja $lesenpenjaja) {
            return $user->id === $lesenpenjaja->user_id;
        });  

        Gate::define('edit-sendiri', function (User $user, LesenPenjaja $lesenpenjaja) {
            return $user->id === $lesenpenjaja->user_id;
        }); 

        Gate::define('keje-admin', function (User $user) {
            return $user->role == 'super';
        }); 

        Gate::define('keje-pt', function (User $user) {
            return $user->role == 'pembantu tadbir';
        }); 

        Gate::define('keje-staff', function (User $user) {
            return in_array($user->role, ['pembantu tadbir','pp kesihatan','pp tadbir','pegawai tadbir','pengarah', 'super']);
        }); 

        Gate::define('sendiri-or-staff', function (User $user, LesenPenjaja $lesenpenjaja) {
            return Gate::forUser($user)->any(['edit-sendiri', 'keje-staff'], $lesenpenjaja);
        }); 
    }
}
