<?php

namespace App\Providers;

use App\Models\Branch\Branch;
use App\Models\Order\Loan\LoanOrder;
use App\Models\Order\Loan\LoanType;
use App\Models\System\Location\Province;
use App\Models\System\Roles\Permission;
use App\Models\System\Roles\Role;
use App\Policies\Branch\BranchPolicy;
use App\Policies\Order\Loan\LoanOrderPolicy;
use App\Policies\Order\Loan\LoanTypePolicy;
use App\Policies\System\Location\ProvincePolicy;
use App\Policies\System\Logs\ActionEventPolicy;
use App\Policies\System\Roles\PermissionPolicy;
use App\Policies\System\Roles\RolePolicy;
use App\Policies\UserPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Laravel\Nova\Actions\ActionEvent;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The model to policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [
        // Orders...
        LoanOrder::class => LoanOrderPolicy::class,

        // Users...
        User::class => UserPolicy::class,

        // Roles and permession
        Role::class => RolePolicy::class,
        Permission::class => PermissionPolicy::class,

        // Loan types...
        LoanType::class => LoanTypePolicy::class,

        // Branches and Provinces...
        Branch::class => BranchPolicy::class,
        Province::class => ProvincePolicy::class,

        // ActionsEvents...
        ActionEvent::class => ActionEventPolicy::class,
    ];

    /**
     * Register any authentication / authorization services.
     */
    public function boot(): void
    {
        // General permissions...
        Gate::define('isMe', fn ($user) => $user->isMe());
        Gate::define('isAdmin', fn ($user) => $user->isAdmin());
        Gate::define('systemUser', fn ($user) => $user->isSystemUser());

        // Tooling permissions...
        Gate::define('viewPulse', fn ($user) => $user->isMe());

        // LoanOrder permissions...
        Gate::define('viewLoanOrders', fn ($user) => $user->isSystemUser());
    }
}
