<?php

namespace App\Modules\HR\Leave\DTOs;

/**
 * Validated payload for creating a leave request (HR or self-service).
 */
final class LeaveRequestSubmitData
{
    private function __construct(
        public readonly int $employeeId,
        public readonly int $leaveTypeId,
        public readonly string $startDate,
        public readonly string $endDate,
        public readonly ?string $reason,
        public readonly bool $isHalfDay,
    ) {}

    /**
     * @param  array<string, mixed>  $validated
     */
    public static function fromValidated(array $validated): self
    {
        return new self(
            (int) $validated['employee_id'],
            (int) $validated['leave_type_id'],
            (string) $validated['start_date'],
            (string) $validated['end_date'],
            isset($validated['reason']) ? (string) $validated['reason'] : null,
            (bool) ($validated['is_half_day'] ?? false),
        );
    }

    /**
     * @return array{employee_id: int, leave_type_id: int, start_date: string, end_date: string, reason: ?string, is_half_day: bool}
     */
    public function toPersistenceArray(): array
    {
        return [
            'employee_id' => $this->employeeId,
            'leave_type_id' => $this->leaveTypeId,
            'start_date' => $this->startDate,
            'end_date' => $this->endDate,
            'reason' => $this->reason,
            'is_half_day' => $this->isHalfDay,
        ];
    }
}
