<?php

namespace KupShop\PohodaBundle\Tests\Coupons;

use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
use KupShop\DevelopmentBundle\Util\Tests\LoggerTestTrait;
use KupShop\PohodaBundle\Messenger\OrderMessage;
use KupShop\PohodaBundle\Tests\TestUtilHelper;
use KupShop\PohodaBundle\Utils\Exporters\CouponExporter;
use KupShop\PohodaBundle\Utils\PohodaConnector;
use KupShop\PohodaBundle\Utils\TransformHelper;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBus;
use Symfony\Component\Messenger\Stamp\HandledStamp;

class CouponsTest extends \DatabaseTestCase
{
    use LoggerTestTrait;
    use TestUtilHelper;
    use CartTestTrait;

    /**
     * @var MessageBus
     */
    private $asyncBus;

    /** @var PohodaConnector */
    private $pohodaConnector;

    protected function setUp(): void
    {
        parent::setUp();
        $this->asyncBus = $this->get('async_bus');
    }

    public function getDataSet()
    {
        return $this->getJsonDataSetFromFile();
    }

    protected $PohodaConnector;
    /**
     * @var TransformHelper
     */
    protected $transformHelper;

    protected function setEnvironment()
    {
        $this->PohodaConnector = $this->get(PohodaConnector::class);
        $this->transformHelper = $this->get(TransformHelper::class);
        $this->getLogger();
    }

    public function testAsyncExportOrder()
    {
        $this->assertTrue(true);
        $couponMock = $this->autowire(CouponExporterMock::class);
        $this->set(CouponExporter::class, $couponMock);

        $instance = $this->autowire(\KupShop\PohodaBundle\Tests\Coupons\PohodaConnectorMock::class);
        $this->set(PohodaConnector::class, $instance);

        $this->prepareMServer();

        $asyncMessage = new OrderMessage();
        $asyncMessage->setIdOrder(4047);

        $result = $this->getHandledResult($this->asyncBus->dispatch($asyncMessage));

        $this->assertNotEmpty($couponMock->couponXml, '');
    }

    public function testCreateCoupons()
    {
        $couponMock = $this->autowire(CouponExporterMock::class);
        $this->set(CouponExporter::class, $couponMock);

        $instance = $this->autowire(\KupShop\PohodaBundle\Tests\Coupons\PohodaConnectorMock::class);
        $this->set(PohodaConnector::class, $instance);

        $this->prepareMServer();

        $this->prepareCart();

        $count = sqlQueryBuilder()->select('COUNT(*)')->from('discounts_coupons')->execute()->fetchOne();
        $this->assertEquals($count, 0, 'Nesmi tam byt zadny poukaz!');

        $this->insertProduct(5644, null, 2);

        $order = $this->submitOrder();

        $count = sqlQueryBuilder()->select('COUNT(*)')->from('discounts_coupons')->execute()->fetchOne();
        $this->assertEquals(2, $count, 'Musi tam byt vygenerovany 2x poukaz!');
        $this->assertEquals(2, count($order->fetchItems()), 'Musi tam byt dve polozky, pro kazdy poukaz jedna!');

        $asyncMessage = new OrderMessage();
        $asyncMessage->setIdOrder($order->id);

        $result = $this->getHandledResult($this->asyncBus->dispatch($asyncMessage));

        $this->assertNotEmpty($couponMock->couponXml, '');
    }

    public function getHandledResult(Envelope $envelope)
    {
        $stamp = $envelope->last(HandledStamp::class);

        return $stamp->getResult();
    }
}

class CouponExporterMock extends CouponExporter
{
    public $couponXml;

    public function transformToRequest(mixed $xml)
    {
        $this->couponXml = parent::transformToRequest($xml);

        return $this->couponXml;
    }
}

class PohodaConnectorMock extends PohodaConnector
{
    public function sendRequest($XmlBodyRequest)
    {
        return true;
    }
}
