<?php

namespace KupShop\ComponentsBundle\Twig\Components\ShoppingList\Forms;

use KupShop\CatalogBundle\Util\FavoriteProductsUtil;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\ComponentInterface;
use KupShop\ComponentsBundle\Twig\Components\ShoppingList\Types\FormType;
use KupShop\ComponentsBundle\Twig\Components\ShoppingList\Utils\Entity;
use KupShop\ComponentsBundle\Twig\ComponentTrait;
use KupShop\ComponentsBundle\Twig\DataProvider\ProductDataTrait;
use KupShop\GraphQLBundle\EventListener\JsShopRefreshListener;
use KupShop\KupShopBundle\Context\UserContext;
use KupShop\ShoppingListBundle\Util\ShoppingListUtil;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent(template: '@Components/components/ShoppingList/Forms/Form.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class Form extends AbstractController implements ComponentInterface
{
    use ComponentTrait;
    use DefaultActionTrait;
    use ComponentWithFormTrait;
    use ComponentToolsTrait;
    use ProductDataTrait;

    #[LiveProp]
    public ?Entity $initialFormData = null;

    protected array $shoppingLists;

    public function __construct(
        private readonly ShoppingListUtil $shoppingList,
        private readonly UserContext $userContext,
        private readonly FavoriteProductsUtil $favoriteProductsUtil,
    ) {
    }

    protected function instantiateForm(): FormInterface
    {
        return $this->createForm(FormType::class, $this->initialFormData, [
            'action' => 'javascript:void(0);',
        ]);
    }

    #[LiveAction]
    public function handleForm()
    {
        $this->submitForm();

        /** @var Entity $entity */
        $entity = $this->getForm()->getData();

        $listId = $this->shoppingList->createShoppingList($entity->newListName);
        if ($this->shoppingList->addProductToShoppingList($listId, $this->id_product, $this->id_variation)) {
            addUserMessage(translate('add_to_shopping_list', 'shopping_list'), 'success');
            $this->dispatchBrowserEvent('modal:close');
        }
    }

    public function getShoppingLists(): ?array
    {
        if (!isset($this->shoppingLists)) {
            $this->shoppingLists = [];
            if ($user = $this->userContext->getActive()) {
                $this->shoppingLists = $user->getShoppingLists();
            }
        }

        return $this->shoppingLists;
    }

    #[LiveAction]
    public function addToFavorites(): void
    {
        if ($this->favoriteProductsUtil->addProduct($this->id_product, $this->userContext->getActiveId(), $this->id_variation)) {
            addUserMessage(translate('add_to_shopping_list', 'shopping_list'), 'success');
            $this->dispatchBrowserEvent('modal:close');
            $this->dispatchBrowserEvent(JsShopRefreshListener::JS_EVENT_NAME, ['scope' => JsShopRefreshListener::PARTIAL_FAVORITES]);
        }
    }

    #[LiveAction]
    public function addToList(#[LiveArg] int $id): void
    {
        if ($this->shoppingList->isUserOwner($id)) {
            if ($this->shoppingList->addProductToShoppingList($id, $this->id_product, $this->id_variation)) {
                addUserMessage(translate('add_to_shopping_list', 'shopping_list'), 'success');
                $this->dispatchBrowserEvent('modal:close');
            }
        }
    }
}
