<?php

declare(strict_types=1);

namespace KupShop\GraphQLBundle\ApiPublic\Types\Content;

use KupShop\ContentBundle\Util\MenuUtil;
use KupShop\GraphQLBundle\ApiShared\ApiUtil;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

#[Type]
readonly class Page
{
    public function __construct(
        private \KupShop\ContentBundle\Entity\Page $page,
    ) {
    }

    #[Field(description: 'ID')]
    public function getId(): int
    {
        return (int) $this->page->getId();
    }

    #[Field(description: 'Typ stránky')]
    public function getType(): int
    {
        return $this->page->getType();
    }

    #[Field(description: 'Název')]
    public function getName(): string
    {
        return $this->page->getName();
    }

    #[Field(description: 'Krátký název')]
    public function getNameShort(): string
    {
        return $this->page->getNameShort();
    }

    #[Field(description: 'URL stránky')]
    public function getUrl(): ?string
    {
        if (empty($this->page->getUrl()) && empty($this->page->getLink())) {
            return null;
        }

        return ($this->page->getType() == MenuUtil::TYPE_PAGE) ? createScriptURL(['URL' => $this->page->getUrl()]) : $this->page->getLink();
    }

    #[Field(description: 'Href target')]
    public function getTarget(): ?string
    {
        if (empty($this->page->getTarget())) {
            return null;
        }

        return $this->page->getTarget();
    }

    /**
     * @return Page[]
     */
    #[Field(description: 'Podstránky')]
    public function getChildren(): array
    {
        return ApiUtil::wrapItems(
            $this->page->getChildren(),
            self::class
        );
    }
}
