<?php
/**
 * For the full copyright and license information, please view the
 * docs/licenses/LICENSE.txt file that was distributed with this source code.
 */

namespace PrestaShop\PrestaShop\Adapter\CMS\PageCategory\CommandHandler;

use CMSCategory;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\CmsPageCategory\Command\AddCmsPageCategoryCommand;
use PrestaShop\PrestaShop\Core\Domain\CmsPageCategory\CommandHandler\AddCmsPageCategoryHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\CmsPageCategory\Exception\CannotAddCmsPageCategoryException;
use PrestaShop\PrestaShop\Core\Domain\CmsPageCategory\Exception\CmsPageCategoryConstraintException;
use PrestaShop\PrestaShop\Core\Domain\CmsPageCategory\Exception\CmsPageCategoryException;
use PrestaShop\PrestaShop\Core\Domain\CmsPageCategory\ValueObject\CmsPageCategoryId;
use PrestaShopException;

/**
 * Adds cms page category
 */
#[AsCommandHandler]
final class AddCmsPageCategoryHandler extends AbstractCmsPageCategoryHandler implements AddCmsPageCategoryHandlerInterface
{
    /**
     * {@inheritdoc}
     *
     * @throws CmsPageCategoryException
     */
    public function handle(AddCmsPageCategoryCommand $command)
    {
        if (!$this->assertHasDefaultLanguage($command->getLocalisedName())) {
            throw new CmsPageCategoryConstraintException('Missing name in default language', CmsPageCategoryConstraintException::MISSING_DEFAULT_LANGUAGE_FOR_NAME);
        }

        if (!$this->assertHasDefaultLanguage($command->getLocalisedFriendlyUrl())) {
            throw new CmsPageCategoryConstraintException('Missing friendly url in default language', CmsPageCategoryConstraintException::MISSING_DEFAULT_LANGUAGE_FOR_FRIENDLY_URL);
        }

        $this->assertIsValidLinkRewrite($command->getLocalisedFriendlyUrl());
        $this->assertDescriptionContainsCleanHtml($command->getLocalisedDescription());

        try {
            $cmsPageCategory = new CMSCategory();
            $cmsPageCategory->name = $command->getLocalisedName();
            $cmsPageCategory->active = $command->isDisplayed();
            $cmsPageCategory->id_parent = $command->getParentId()->getValue();
            $cmsPageCategory->description = $command->getLocalisedDescription();
            $cmsPageCategory->meta_title = $command->getLocalisedMetaTitle();
            $cmsPageCategory->meta_description = $command->getLocalisedMetaDescription();

            $cmsPageCategory->link_rewrite = $command->getLocalisedFriendlyUrl();

            if (false === $cmsPageCategory->add()) {
                throw new CannotAddCmsPageCategoryException('Failed to add cms page category');
            }

            $this->associateWithShops($cmsPageCategory, $command->getShopAssociation());
        } catch (PrestaShopException $exception) {
            throw new CmsPageCategoryException('An unexpected error occurred when adding cms page category', 0, $exception);
        }

        return new CmsPageCategoryId((int) $cmsPageCategory->id);
    }
}
