<?php

declare(strict_types=1);

namespace KupShop\GraphQLBundle\ApiPublic\Types\Search;

use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

/**
 * @Type()
 */
class AutocompleteResultItem
{
    public function __construct(
        protected string $type,
        protected array $item,
        protected string $photoPlaceholder,
    ) {
    }

    #[Field]
    public function getTitle(): string
    {
        // :-(
        if ($this->type == \FulltextSearch::INDEX_PAGES) {
            return $this->item['name'];
        }

        return $this->item['title'];
    }

    #[Field]
    public function getUrl(): string
    {
        $redirectTypes = [
            \FulltextSearch::INDEX_PRODUCTS => 'product',
            \FulltextSearch::INDEX_SECTIONS => 'section',
            \FulltextSearch::INDEX_PAGES => 'page',
            \FulltextSearch::INDEX_ARTICLES => 'article',
            \FulltextSearch::INDEX_PRODUCERS => 'producer',
        ];

        return path('kupshop_content_redirect_redirect', ['type' => $redirectTypes[$this->type], 'id' => $this->item['id']]);
    }

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

    #[Field]
    public function getPhoto(): ?string
    {
        return $this->item['photo'] ?? $this->photoPlaceholder ?? '';
    }
}
