<?php

namespace App\Modules\HR\Recruitment\DTOs;

final readonly class ApplicationWriteData
{
    public function __construct(
        public int $vacancyId,
        public int $candidateId,
        public ?int $candidateCvId,
        public ?string $source,
        public ?string $coverLetter,
        public ?string $status = null,
    ) {}

    /**
     * @param  array<string, mixed>  $data
     */
    public static function fromValidated(array $data): self
    {
        return new self(
            vacancyId: (int) $data['vacancy_id'],
            candidateId: (int) $data['candidate_id'],
            candidateCvId: isset($data['candidate_cv_id']) ? (int) $data['candidate_cv_id'] : null,
            source: isset($data['source']) ? (string) $data['source'] : null,
            coverLetter: isset($data['cover_letter']) ? (string) $data['cover_letter'] : null,
            status: isset($data['status']) ? (string) $data['status'] : null,
        );
    }

    /**
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        $payload = [
            'vacancy_id' => $this->vacancyId,
            'candidate_id' => $this->candidateId,
            'candidate_cv_id' => $this->candidateCvId,
            'source' => $this->source,
            'cover_letter' => $this->coverLetter,
        ];
        if ($this->status !== null) {
            $payload['status'] = $this->status;
        }

        return $payload;
    }
}
