<?php

namespace KupShop\B2BBundle\Twig\Components\B2BUserRegistration;

use KupShop\ComponentsBundle\Attributes\Blocek;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\Components\Utils\Forms\EmailForm;
use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Util\System\UrlFinder;
use Symfony\Component\Form\FormInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\ComponentWithFormTrait;

#[AsLiveComponent(template: '@B2B/components/B2BUserRegistrationForm/Form.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
#[Blocek(title: 'Registrační formulář (B2B)')]
class Form extends EmailForm
{
    use ComponentWithFormTrait;

    public function __construct(
        private readonly UserContext $userContext,
        private readonly UrlFinder $urlFinder,
        protected TranslatorInterface $translator,
    ) {
    }

    #[LiveProp]
    public bool $showAdditionalAddress = false;

    #[LiveAction]
    public function toggleAdditionalAddress(): void
    {
        $this->showAdditionalAddress = !$this->showAdditionalAddress;
    }

    private function processUrl($params): string
    {
        return htmlspecialchars(createScriptURL($params), ENT_QUOTES);
    }

    public function getConsentUrl(): string
    {
        return $this->processUrl(['s' => 'page', 'label' => 'consent']);
    }

    public function getPrivacyPolicyUrl(): string
    {
        return $this->processUrl(['s' => 'page', 'label' => 'privacy_policy']);
    }

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

    public function getIsUserLogged(): bool
    {
        return !empty($this->userContext->getActiveId());
    }

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

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

        try {
            $user = $this->userContext->getActive();

            // Report
            $this->sendEmail(B2BUserRegistrationEmail::getType(), function (B2BUserRegistrationEmail $emailService) use ($entity, $user) {
                $userEmail = $entity->email ?? $user->email;
                $emailService->setSender($userEmail);
                $emailService->setData([
                    'DATA_NAME' => $entity->name,
                    'DATA_SURNAME' => $entity->surname,
                    'DATA_EMAIL' => $userEmail,
                    'DATA_INVOICE_EMAIL' => $entity->invoice_email ?? '',
                    'DATA_COMPANY' => $entity->company,
                    'DATA_PHONE' => $entity->phone,
                    'DATA_ICO' => $entity->ico,
                    'DATA_DIC' => $entity->dic,
                    'DATA_STREET' => $entity->street,
                    'DATA_CITY' => $entity->city,
                    'DATA_PSC' => $entity->psc,
                    'DATA_COUNTRY' => $entity->country,
                    'DATA_ADDITIONAL_ADDRESS' => $entity->additional_address ?? '',
                    'DATA_INAME' => $entity->iname ?? '',
                    'DATA_ISURNAME' => $entity->isurname ?? '',
                    'DATA_ISTREET' => $entity->istreet ?? '',
                    'DATA_ICITY' => $entity->icity ?? '',
                    'DATA_IPSC' => $entity->ipsc ?? '',
                ]);

                return $emailService;
            });

            // Answer
            $this->sendEmail(B2BUserRegistrationAnswerEmail::getType(), function (B2BUserRegistrationAnswerEmail $emailService) use ($entity, $user) {
                $userEmail = $entity->email ?? $user->email;
                $emailService->setSender($userEmail);

                return $emailService;
            });

            addUserMessage($this->translator->trans('form.success', [], 'forms'), 'success');
        } catch (\Exception $e) {
            addUserMessage($this->translator->trans('form.error', [], 'forms'), 'error');

            addUserMessage($e->getMessage(), 'error');
        }

        $this->resetForm();
    }
}
