<?php

namespace App\Modules\HR\Recruitment\DTOs;

final readonly class VacancyWriteData
{
    public function __construct(
        public int $jobRequestId,
        public int $departmentId,
        public ?int $positionId,
        public string $title,
        public ?string $description,
        public int $openings,
        public ?string $closesAt,
    ) {}

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

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