<?php

namespace App\Modules\HR\Recruitment\Policies;

use App\Models\User;
use App\Modules\HR\Recruitment\Models\RecruitmentVacancy;
use App\Modules\HR\Recruitment\Support\VacancyStatus;

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

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

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

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

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

    public function publish(User $user, RecruitmentVacancy $vacancy): bool
    {
        return (int) $user->tenant_id === (int) $vacancy->tenant_id
            && $user->can('recruitment.manage')
            && $vacancy->status === VacancyStatus::DRAFT;
    }

    public function close(User $user, RecruitmentVacancy $vacancy): bool
    {
        return (int) $user->tenant_id === (int) $vacancy->tenant_id
            && $user->can('recruitment.manage')
            && in_array($vacancy->status, [VacancyStatus::OPEN, VacancyStatus::ON_HOLD], true);
    }
}
