<?php

declare(strict_types=1);

namespace Seka\Vzdelavanie\Dtos\Kniznica;

use Elceka\Laravel\HashId\Facades\Hasher;
use Illuminate\Contracts\Support\Arrayable;
use Seka\Vzdelavanie\Models\Kniznica\Sekcia;

/**
 * Class SekciaDTO
 *
 * @package Seka\Vzdelavanie\Dtos\Kniznica
 */
class SekciaDTO implements Arrayable
{
    /**
     * @var int|null
     */
    public ?int $id;

    /**
     * @var int|null
     */
    public ?int $parent_id;

    /**
     * @var int|null
     */
    public ?int $ucebnica_id;

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

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

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

    /**
     * @var \Seka\Vzdelavanie\Dtos\Kniznica\UcebnicaDTO|null
     */
    public ?UcebnicaDTO $ucebnica;

    /**
     * @var \Seka\Vzdelavanie\Dtos\Kniznica\SekciaDTO|null
     */
    public ?self $parent;

    /**
     * @var \Seka\Vzdelavanie\Dtos\Kniznica\SekciaDTO[]|null
     */
    public ?array $children;

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

    /**
     * @var int|null
     */
    public ?int $_lft;

    /**
     * @var int|null
     */
    public ?int $_rgt;

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

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

    /**
     * SekciaDTO constructor.
     *
     * @param array $parameters
     *
     * @throws \Spatie\DataTransferObject\Exceptions\UnknownProperties
     * @throws \Spatie\DataTransferObject\Exceptions\ValidationException
     */
    public function __construct(array $parameters = [])
    {
        $this->id = $parameters['id'] ?? null;
        $this->parent_id = $parameters['parent_id'] ?? null;
        $this->ucebnica_id = $parameters['ucebnica_id'] ?? null;
        $this->hash_id = $parameters['hash_id'] ?? null;
        $this->nazov = $parameters['nazov'] ?? null;
        $this->obsah = $parameters['obsah'] ?? null;
        $this->_lft = $parameters['_lft'] ?? null;
        $this->_rgt = $parameters['_rgt'] ?? null;
        $this->created_at = $parameters['created_at'] ?? null;
        $this->updated_at = $parameters['updated_at'] ?? null;
        $this->children = null;
        $this->parent = null;
        $this->ucebnica = null;

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

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

        if (isset($parameters['children'])) {
            $this->children = collect($parameters['children'])->map(function ($sekcia) {
                return $sekcia instanceof static ? $sekcia : new static($sekcia);
            })->toArray();
        }

        $this->link = $this->makeLink();
    }

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

    /**
     * @return string
     */
    protected function makeLink(): string
    {
        return route(
            'vzdelavanie.kniznica.ucebnica.sekcia.show',
            [
                Hasher::encode($this->ucebnica_id),
                $this->hash_id,
            ],
        );
    }

    /**
     * @return array
     */
    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'parent_id' => $this->parent_id,
            'ucebnica_id' => $this->ucebnica_id,
            'hash_id' => $this->hash_id,
            'nazov' => $this->nazov,
            'obsah' => $this->obsah,
            'ucebnica' => $this->ucebnica?->toArray(),
            'parent' => $this->parent,
            'children' => $this->children,
            'link' => $this->link,
            'lft' => $this->_lft,
            'rgt' => $this->_rgt,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}
