<?php

namespace KupShop\ArticlesBundle\Twig\Components\Articles\Detail;

use KupShop\ArticlesBundle\Twig\Components\Articles\Utils\ArticleTrait;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Entity\Thumbnail;
use KupShop\ComponentsBundle\Interfaces\Microdata;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\DataProvider\MicrodataProvider;
use KupShop\KupShopBundle\Util\Compat\CtrlLoader;
use KupShop\KupShopBundle\Util\System\UrlFinder;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Articles/components/Articles/Detail/Header/Header.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class Header extends BaseComponent implements Microdata
{
    use ArticleTrait {
        getUrl as getBaseUrl;
    }
    public array $article;

    public function __construct(
        private readonly UrlFinder $urlFinder,
        private readonly CtrlLoader $ctrlLoader,
    ) {
    }

    public function getUrl(): string
    {
        return $this->getBaseUrl($this->article['id'], null, 'article', 'IDa', 1);
    }

    public function getImage(): ?array
    {
        return $this->article['image'] ?? null;
    }

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

    public function getTitle(): string
    {
        return $this->article['title'];
    }

    public function getSeen(): string
    {
        return $this->article['seen'];
    }

    public function getSectionID(): string
    {
        return $this->article['id_branch'];
    }

    public function getAuthor(): ?string
    {
        if (empty($this->article['authors'])) {
            echo 'nevim';

            return null;
        }
        $firstAuthor = reset($this->article['authors']);

        if (!empty($firstAuthor['nick'])) {
            return $firstAuthor['nick'];
        }

        return $firstAuthor['name'].' '.$firstAuthor['surname'];
    }

    public function getAuthorUrl(): ?string
    {
        if (!empty($this->article['authors'])) {
            $firstAuthor = reset($this->article['authors']);

            return path('kupshop_content_articles_articlesauthor', [
                'authorId' => $firstAuthor['id'],
            ]);
        }

        return null;
    }

    public function getDate(): string
    {
        return $this->article['date'];
    }

    public function getLead(): string
    {
        return $this->article['lead'];
    }

    public function setArticle(array $article): void
    {
        $this->article = $article;
    }

    public function initMicrodata(): array
    {
        $author = ['@type' => 'Person'];
        if (count($_authors = $this->getAuthor()) > 1) {
            foreach ($_authors as $_author) {
                $author['authors']['name'] = $_author['name'].' '.$_author['surname'];
                break;
            }
        }

        $dbcfg = \Settings::getDefault();

        if (empty($author['authors']['name'])) {
            $author['authors']['name'] = $dbcfg['shop_firm_name'];
        }

        $articleMicrodata = [
            '@context' => 'http://schema.org',
            '@type' => 'NewsArticle',
            'headline' => $this->getTitle(),
            'author' => $author,
            'url' => $this->urlFinder->shopUrlAbsolute($this->ctrlLoader['currUrl']['Abs']),
            'publisher' => [
                '@type' => 'Organization',
                'name' => $dbcfg['shop_firm_name'],
                'logo' => [
                    '@type' => 'ImageObject',
                    'url' => $this->urlFinder->staticUrlAbsolute('/templates/images/logo.png'),
                ],
            ],
        ];

        if ($this->getImage()) {
            $articleMicrodata['image'] = $this->urlFinder->shopUrlAbsolute($this->getImage()['src']);
        }

        if ($this->getDate()) {
            $articleMicrodata['datePublished'] = $this->getDate();
        }

        if ($this->getLead()) {
            $articleMicrodata['description'] = $this->getLead();
        }

        return [MicrodataProvider::ARTICLE => $articleMicrodata];
    }

    public function getTag(): ?string
    {
        if (!empty($this->article['tags'])) {
            $firstTag = reset($this->article['tags']);

            return $firstTag['tag'] ?? '';
        }

        return null;
    }

    public function getTagUrl($idKey = 'ID', $absolute = null): ?string
    {
        if (!empty($this->article['tags'])) {
            $tag = reset($this->article['tags']);
            $params = [
                's' => 'articles_tags',
                $idKey => $tag['id'],
                'TITLE' => $tag['tag'],
            ];

            if ($absolute) {
                $params['absolute'] = 1;
            }

            return htmlspecialchars(createScriptURL($params), ENT_QUOTES);
        }

        return null;
    }

    public function getThumbnail(): ?Thumbnail
    {
        if (!empty($this->article['image'])) {
            return new Thumbnail((string) $this->article['image']['id'], $this->article['image']['descr'], (string) $this->article['image']['date_update']);
        }

        return null;
    }
}
