<?php

declare(strict_types=1);

namespace Seka\Vzdelavanie\Dtos\Kniznica;

use Illuminate\Contracts\Support\Arrayable;
use Seka\Vzdelavanie\Enums\ModelStatusEnum;
use Seka\Vzdelavanie\Models\Kniznica\Ucebnica;

/**
 * Class UcebnicaDTO
 *
 * @package Seka\Vzdelavanie\Dtos\Kniznica
 */
final class UcebnicaDTO implements Arrayable
{
    /**
     * @var int
     */
    public int $id;

    /**
     * @var int
     */
    public int $kategoria_id;

    /**
     * @var string
     */
    public string $hash_id;

    /**
     * @var string
     */
    public string $nazov;

    /**
     * @var string
     */
    public string $autor;

    /**
     * @var string|null
     */
    public ?string $popis;

    /**
     * @var string|null
     */
    public ?string $obrazok;

    /**
     * @var \Seka\Vzdelavanie\Dtos\Kniznica\KategoriaDTO|null
     */
    public ?KategoriaDTO $kategoria;

    /**
     * @var \Seka\Vzdelavanie\Enums\ModelStatusEnum
     */
    public ModelStatusEnum $stav;

    /**
     * @var string
     */
    public string $created_at;

    /**
     * @var string
     */
    public string $updated_at;

    /**
     * UcebnicaDTO constructor.
     *
     * @param array $parameters
     *
     */
    public function __construct(array $parameters = [])
    {
        $this->id = (int) ($parameters['id'] ?? 0);
        $this->kategoria_id = (int) ($parameters['kategoria_id'] ?? 0);
        $this->hash_id = (string) ($parameters['hash_id'] ?? '');
        $this->nazov = (string) ($parameters['nazov'] ?? '');
        $this->autor = (string) ($parameters['autor'] ?? '');
        $this->popis = $parameters['popis'] ?? null;
        $this->obrazok = $parameters['obrazok'] ?? null;
        $this->stav = $parameters['stav'] instanceof ModelStatusEnum
            ? $parameters['stav']
            : ModelStatusEnum::from($parameters['stav'] ?? '');
        $this->created_at = (string) ($parameters['created_at'] ?? '');
        $this->updated_at = (string) ($parameters['updated_at'] ?? '');
        $this->kategoria = null;

        if (isset($parameters['kategoria'])) {
            $this->kategoria = $parameters['kategoria'] instanceof KategoriaDTO
                ? $parameters['kategoria']
                : new KategoriaDTO($parameters['kategoria']);
        }
    }

    /**
     * @param \Seka\Vzdelavanie\Models\Kniznica\Ucebnica $model
     *
     * @return static
     * @throws \Spatie\DataTransferObject\Exceptions\UnknownProperties
     * @throws \Spatie\DataTransferObject\Exceptions\ValidationException
     */
    public static function fromModel(Ucebnica $model): self
    {
        return new self($model->toArray());
    }

    /**
     * @return array
     */
    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'kategoria_id' => $this->kategoria_id,
            'hash_id' => $this->hash_id,
            'nazov' => $this->nazov,
            'autor' => $this->autor,
            'popis' => $this->popis,
            'obrazok' => $this->obrazok,
            'kategoria' => $this->kategoria,
            'stav' => $this->stav,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}
