<?php

namespace App\Modules\HR\PermissionRequests\Policies;

use App\Models\User;
use App\Modules\HR\PermissionRequests\Models\PermissionRequest;
use App\Modules\HR\PermissionRequests\Support\PermissionRequestStatus;

class PermissionRequestPolicy
{
    public function viewAny(User $user): bool
    {
        return $user->can('permission_requests.view');
    }

    public function view(User $user, PermissionRequest $request): bool
    {
        if ((int) $user->tenant_id !== (int) $request->tenant_id) {
            return false;
        }
        if ($user->employee_id !== null && (int) $user->employee_id === (int) $request->employee_id) {
            return true;
        }

        return $user->can('permission_requests.view');
    }

    public function create(User $user): bool
    {
        if ($user->can('permission_requests.create')) {
            return true;
        }

        return ! $user->is_admin
            && $user->tenant_id !== null
            && $user->employee_id !== null;
    }

    public function review(User $user, PermissionRequest $request): bool
    {
        return $request->status === PermissionRequestStatus::PENDING
            && $user->can('permission_requests.review');
    }
}
