<?php

namespace App\Services\Recruitment;

use App\Models\Applicant;
use Carbon\Carbon;

class RecruitmentSlaService
{
    public function dueAtForNewApplicant(?Carbon $appliedAt = null): Carbon
    {
        $days = max(1, (int) config('recruitment.screening_sla_days', 5));

        return ($appliedAt ?? now())->copy()->addDays($days)->endOfDay();
    }

    public function isOverdue(Applicant $applicant): bool
    {
        if (! in_array($applicant->stage, ['applied', 'screening'], true)) {
            return false;
        }

        if (! $applicant->sla_due_at) {
            return false;
        }

        return $applicant->sla_due_at->isPast();
    }

    public function clearOnAdvance(Applicant $applicant): void
    {
        if (! in_array($applicant->stage, ['applied', 'screening'], true)) {
            $applicant->sla_due_at = null;
        }
    }
}
