<?php

namespace App\Modules\HR\Recruitment\DTOs;

final readonly class JobRequestWriteData
{
    public function __construct(
        public int $departmentId,
        public ?int $positionId,
        public string $title,
        public int $openings,
        public ?string $justification,
    ) {}

    /**
     * @param  array<string, mixed>  $data
     */
    public static function fromValidated(array $data): self
    {
        return new self(
            departmentId: (int) $data['department_id'],
            positionId: isset($data['position_id']) ? (int) $data['position_id'] : null,
            title: (string) $data['title'],
            openings: (int) ($data['openings'] ?? 1),
            justification: isset($data['justification']) ? (string) $data['justification'] : null,
        );
    }

    /**
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'department_id' => $this->departmentId,
            'position_id' => $this->positionId,
            'title' => $this->title,
            'openings' => $this->openings,
            'justification' => $this->justification,
        ];
    }
}
