<?php

declare(strict_types=1);

namespace KupShop\ShoppingListBundle\Email;

use KupShop\KupShopBundle\Email\BaseEmail;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\ShoppingListBundle\Util\ShoppingListUtil;
use Symfony\Contracts\Service\Attribute\Required;

class ShareShoppingListEmail extends BaseEmail
{
    protected static $name = 'Sdílení nákupního seznamu';
    protected static $type = 'SHARE_SHOPPING_LIST';
    protected static $priority = -20;

    protected $subject = 'Můj nákupní seznam';
    protected $template = 'email/email_share_shopping_list.tpl';

    public ShoppingListUtil $shoppingListUtil;
    public int $listId;
    public string $from;

    #[Required]
    final public function setShoppingListUtil(ShoppingListUtil $shoppingListUtil): void
    {
        $this->shoppingListUtil = $shoppingListUtil;
    }

    public function setListId(int $listId): void
    {
        $this->listId = $listId;
    }

    public function setFrom(string $from): void
    {
        $this->from = $from;
    }

    public function testEmail(): array
    {
        $listId = sqlQueryBuilder()
            ->select('id')
            ->from('shopping_list')
            ->setMaxResults(1)
            ->execute()->fetchOne();

        $shoppingList = $this->shoppingListUtil->getShoppingList($listId);
        $this->setListId($shoppingList->getId());
        $this->setFrom('Testovací Uživatel');

        return parent::testEmail();
    }

    public function __wakeup()
    {
        parent::__wakeup();

        $this->shoppingListUtil = ServiceContainer::getService(ShoppingListUtil::class);
    }

    public function __sleep()
    {
        return array_diff(parent::__sleep(), ['shoppingListUtil']);
    }
}
