<?php

namespace App\Domain\Workflow\Requirements\Checks;

use App\Domain\Applicants\Models\Applicant;
use App\Domain\Workflow\Requirements\StageRequirement;

/**
 * A confirmed job demand — and with it an employer — before visa or deployment
 * work begins.
 */
class JobDemandSelected implements StageRequirement
{
    public function type(): string
    {
        return 'job_demand_selected';
    }

    public function describe(array $params): string
    {
        return ($params['require_open'] ?? false)
            ? 'A job demand that is still open must be selected.'
            : 'A job demand must be selected.';
    }

    public function isSatisfiedBy(Applicant $applicant, array $params): bool
    {
        if ($applicant->job_demand_id === null) {
            return false;
        }

        if (! ($params['require_open'] ?? false)) {
            return true;
        }

        return $applicant->jobDemand?->status->acceptsCandidates() ?? false;
    }
}
