<?php

declare(strict_types=1);

namespace External\HannahBundle\Admin\Actions;

use External\HannahBundle\Email\OCUserCardEmail;
use External\HannahBundle\Util\User\UserUtil;
use KupShop\AdminBundle\Admin\Actions\AbstractAction;
use KupShop\AdminBundle\Admin\Actions\ActionResult;
use KupShop\AdminBundle\Admin\Actions\IAction;
use KupShop\KupShopBundle\Context\ContextManager;
use KupShop\KupShopBundle\Context\DomainContext;
use KupShop\KupShopBundle\Context\LanguageContext;
use Symfony\Contracts\Service\Attribute\Required;

class UserSendCardPDFAction extends AbstractAction implements IAction
{
    #[Required]
    public ContextManager $contextManager;
    #[Required]
    public OCUserCardEmail $userCardEmail;
    #[Required]
    public UserUtil $userUtil;

    public function getTypes(): array
    {
        return ['users'];
    }

    public function getName(): string
    {
        return '[OC] Odeslat zákaznickou kartu';
    }

    public function execute(&$data, array $config, string $type): ActionResult
    {
        if (!$this->userUtil->getUserCard((int) $this->getId())) {
            return new ActionResult(false, 'Uživatel nemá přiřazenou žádnou kartu!');
        }

        $user = \User::createFromId((int) $this->getId());

        $message = $this->contextManager->activateContexts(
            [
                LanguageContext::class => $user->id_language ?? null,
                DomainContext::class => $this->contextManager->getDomainFromLanguage($user->id_language ?? null),
            ],
            function () use ($user) {
                $this->userCardEmail->setUser($user);

                return $this->userCardEmail->getEmail();
            }
        );

        $message['to'] = $user['email'];

        $this->userCardEmail->sendEmail($message);

        return new ActionResult(true);
    }
}
