<?php

declare(strict_types=1);

namespace KupShop\GraphQLBundle\ApiAdmin\Controller\Content;

use KupShop\GraphQLBundle\ApiAdmin\Annotation\Module;
use KupShop\GraphQLBundle\ApiAdmin\Types\Collection\ArticleCollection;
use KupShop\GraphQLBundle\ApiAdmin\Types\Content\Article\Article;
use KupShop\GraphQLBundle\ApiAdmin\Types\Content\Article\Input\ArticleFilterInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\Content\Article\Input\ArticleTranslationInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\Content\Article\Response\ArticleTranslationMutateResponse;
use KupShop\GraphQLBundle\ApiAdmin\Types\MutateResponse;
use KupShop\GraphQLBundle\ApiAdmin\Types\Parameters;
use KupShop\GraphQLBundle\ApiAdmin\Util\Content\ArticleUtil;
use TheCodingMachine\GraphQLite\Annotations\Mutation;
use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\GraphQLite\Annotations\UseInputType;

readonly class ArticleController
{
    public function __construct(
        private ArticleUtil $articleUtil,
    ) {
    }

    /**
     * Načte článek podle ID.
     */
    #[Query]
    public function article(int $id): ?Article
    {
        return $this->articleUtil->getArticle($id);
    }

    /**
     * Seznam článků.
     */
    #[Query]
    public function articles(int $offset = 0, int $limit = 100, #[UseInputType('ArticleSortInput')] ?Parameters $sort = null, ?ArticleFilterInput $filter = null): ArticleCollection
    {
        return $this->articleUtil->getArticles($offset, $limit, $sort, $filter);
    }

    /**
     * Aktualizuje překlad článku.
     */
    #[Mutation]
    #[Module(\Modules::TRANSLATIONS)]
    public function articleTranslate(ArticleTranslationInput $input): ArticleTranslationMutateResponse
    {
        return $this->articleUtil->updateArticleTranslations($input);
    }

    /**
     * Hromadně aktualizuje hodnoty překladů u článků (doporučeno při aktualizaci více článků najednou).
     *
     * @param ArticleTranslationInput[] $input
     */
    #[Mutation]
    #[Module(\Modules::TRANSLATIONS)]
    public function articleTranslateBulkUpdate(array $input): MutateResponse
    {
        foreach ($input as $parameterInput) {
            $this->articleUtil->updateArticleTranslations($parameterInput);
        }

        return new MutateResponse(true);
    }
}
