<?php

namespace App\Modules\Inventory\DTOs;

final class ItemWriteData
{
    public function __construct(
        public readonly string $name,
        public readonly ?int $inventoryCategoryId,
        public readonly ?string $barcode,
        public readonly ?string $description,
    ) {}

    /**
     * @param  array<string, mixed>  $validated
     */
    public static function fromValidated(array $validated): self
    {
        return new self(
            name: (string) $validated['name'],
            inventoryCategoryId: ! empty($validated['inventory_category_id'])
                ? (int) $validated['inventory_category_id']
                : null,
            barcode: isset($validated['barcode']) ? (string) $validated['barcode'] : null,
            description: isset($validated['description']) ? (string) $validated['description'] : null,
        );
    }
}
