<?php

namespace App\Modules\HR\Recruitment\DTOs;

final readonly class OfferLetterWriteData
{
    public function __construct(
        public int $applicationId,
        public int $candidateId,
        public int $vacancyId,
        public ?int $departmentId,
        public ?int $positionId,
        public string $employmentType,
        public ?int $employmentStatusId,
        public string $startDate,
        public ?int $probationPeriodDays,
        public string $salary,
        public string $currency,
        public ?string $allowances,
        public ?string $benefits,
        public ?string $workingHours,
        public ?string $notes,
        public ?string $expiresAt,
        public ?string $acceptanceDeadlineAt,
    ) {}

    /** @param  array<string, mixed>  $data */
    public static function fromValidated(array $data): self
    {
        return new self(
            applicationId: (int) $data['application_id'],
            candidateId: (int) $data['candidate_id'],
            vacancyId: (int) $data['vacancy_id'],
            departmentId: isset($data['department_id']) ? (int) $data['department_id'] : null,
            positionId: isset($data['position_id']) ? (int) $data['position_id'] : null,
            employmentType: (string) $data['employment_type'],
            employmentStatusId: isset($data['employment_status_id']) ? (int) $data['employment_status_id'] : null,
            startDate: (string) $data['start_date'],
            probationPeriodDays: isset($data['probation_period_days']) ? (int) $data['probation_period_days'] : null,
            salary: number_format((float) $data['salary'], 2, '.', ''),
            currency: strtoupper((string) $data['currency']),
            allowances: isset($data['allowances']) ? (string) $data['allowances'] : null,
            benefits: isset($data['benefits']) ? (string) $data['benefits'] : null,
            workingHours: isset($data['working_hours']) ? (string) $data['working_hours'] : null,
            notes: isset($data['notes']) ? (string) $data['notes'] : null,
            expiresAt: isset($data['expires_at']) ? (string) $data['expires_at'] : null,
            acceptanceDeadlineAt: isset($data['acceptance_deadline_at']) ? (string) $data['acceptance_deadline_at'] : null,
        );
    }

    /** @return array<string, mixed> */
    public function toArray(): array
    {
        return [
            'application_id' => $this->applicationId,
            'candidate_id' => $this->candidateId,
            'vacancy_id' => $this->vacancyId,
            'department_id' => $this->departmentId,
            'position_id' => $this->positionId,
            'employment_type' => $this->employmentType,
            'employment_status_id' => $this->employmentStatusId,
            'start_date' => $this->startDate,
            'probation_period_days' => $this->probationPeriodDays,
            'salary' => $this->salary,
            'currency' => $this->currency,
            'allowances' => $this->allowances,
            'benefits' => $this->benefits,
            'working_hours' => $this->workingHours,
            'notes' => $this->notes,
            'expires_at' => $this->expiresAt,
            'acceptance_deadline_at' => $this->acceptanceDeadlineAt,
        ];
    }
}
