<?php

namespace App\Modules\HR\Recruitment\Services;

use App\Modules\HR\Recruitment\Models\RecruitmentOfferLetter;
use Carbon\Carbon;

final class HiringEmployeePrefillService
{
    /**
     * @return array<string, mixed>
     */
    public function fromOffer(RecruitmentOfferLetter $offer): array
    {
        $offer->loadMissing(['candidate']);
        $candidate = $offer->candidate;
        $hireDate = $offer->start_date?->format('Y-m-d') ?? now()->toDateString();
        $probationEnd = null;
        if ($offer->probation_period_days !== null && (int) $offer->probation_period_days > 0) {
            $probationEnd = Carbon::parse($hireDate)
                ->addDays((int) $offer->probation_period_days)
                ->toDateString();
        }

        return [
            'full_name' => $candidate?->full_name ?? '',
            'phone_1' => $candidate?->phone ?? '',
            'email' => $candidate?->email,
            'nationality' => $candidate?->nationality,
            'department_id' => $offer->department_id,
            'position_id' => $offer->position_id,
            'employment_status_id' => $offer->employment_status_id,
            'salary' => (string) $offer->salary,
            'hire_date' => $hireDate,
            'probation_end_date' => $probationEnd,
            'gender' => old('gender'),
            'national_id' => old('national_id'),
        ];
    }
}
