<?php

namespace App\Modules\HR\Positions\DTOs;

final readonly class PositionWriteData
{
    public function __construct(
        public string $title,
        public int $departmentId,
        public string $salary,
        public ?string $description,
        public bool $isActive,
    ) {}

    /**
     * @param  array{title: string, department_id: int, salary: numeric|string, description?: ?string, is_active?: bool}  $data
     */
    public static function fromValidated(array $data): self
    {
        $description = $data['description'] ?? null;

        return new self(
            title: (string) $data['title'],
            departmentId: (int) $data['department_id'],
            salary: (string) $data['salary'],
            description: $description !== null && $description !== '' ? (string) $description : null,
            isActive: (bool) ($data['is_active'] ?? true),
        );
    }

    /**
     * @return array{title: string, department_id: int, salary: string, description: ?string, is_active: bool}
     */
    public function toArray(): array
    {
        return [
            'title' => $this->title,
            'department_id' => $this->departmentId,
            'salary' => $this->salary,
            'description' => $this->description,
            'is_active' => $this->isActive,
        ];
    }
}
