<?php

namespace App\Modules\HR\Recruitment\Policies;

use App\Models\User;
use App\Modules\HR\Recruitment\Models\RecruitmentJobRequest;
use App\Modules\HR\Recruitment\Support\JobRequestStatus;

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

    public function view(User $user, RecruitmentJobRequest $jobRequest): bool
    {
        return (int) $user->tenant_id === (int) $jobRequest->tenant_id
            && $user->can('recruitment.view');
    }

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

    public function update(User $user, RecruitmentJobRequest $jobRequest): bool
    {
        return (int) $user->tenant_id === (int) $jobRequest->tenant_id
            && $user->can('recruitment.manage')
            && $jobRequest->status === JobRequestStatus::DRAFT;
    }

    public function delete(User $user, RecruitmentJobRequest $jobRequest): bool
    {
        return (int) $user->tenant_id === (int) $jobRequest->tenant_id
            && $user->can('recruitment.manage');
    }

    public function submit(User $user, RecruitmentJobRequest $jobRequest): bool
    {
        return (int) $user->tenant_id === (int) $jobRequest->tenant_id
            && $user->can('recruitment.manage')
            && $jobRequest->status === JobRequestStatus::DRAFT;
    }

    public function approve(User $user, RecruitmentJobRequest $jobRequest): bool
    {
        return (int) $user->tenant_id === (int) $jobRequest->tenant_id
            && $user->can('recruitment.manage')
            && $jobRequest->status === JobRequestStatus::PENDING_APPROVAL;
    }
}
