<?php

namespace App\Modules\HR\Departments\DTOs;

final readonly class DepartmentWriteData
{
    public function __construct(
        public string $name,
        public ?int $parentId,
        public ?string $description,
        public bool $isActive,
    ) {}

    /**
     * @param  array{name: string, parent_id?: int|null, description?: string|null, is_active?: bool}  $data
     */
    public static function fromValidated(array $data): self
    {
        return new self(
            name: $data['name'],
            parentId: isset($data['parent_id']) && $data['parent_id'] !== null
                ? (int) $data['parent_id']
                : null,
            description: $data['description'] ?? null,
            isActive: (bool) ($data['is_active'] ?? true),
        );
    }

    /**
     * @return array{name: string, parent_id: int|null, description: string|null, is_active: bool}
     */
    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'parent_id' => $this->parentId,
            'description' => $this->description,
            'is_active' => $this->isActive,
        ];
    }
}
