<?php

namespace App\Domain\Workflow\Requirements\Checks;

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

/**
 * A passport that is present and still valid, optionally with a minimum
 * remaining validity — most destinations refuse a visa on a passport expiring
 * within six months.
 */
class PassportValid implements StageRequirement
{
    public function type(): string
    {
        return 'passport_valid';
    }

    public function describe(array $params): string
    {
        $months = $params['minimum_months'] ?? null;

        return $months
            ? "A passport valid for at least {$months} more months."
            : 'A valid, unexpired passport.';
    }

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

        $months = (int) ($params['minimum_months'] ?? 0);

        if ($months <= 0) {
            return true;
        }

        return $applicant->passport_expiry_date->isAfter(now()->addMonths($months));
    }
}
