<?php

declare(strict_types=1);

namespace KupShop\WatchdogBundle\Tests;

use KupShop\DevelopmentBundle\MailArchive;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\WatchdogBundle\Util\Watchdog;

class WatchdogPriceTest extends \DatabaseTestCase
{
    private Watchdog $watchdog;
    private MailArchive $mailArchive;

    protected function setUp(): void
    {
        parent::setUp();

        $this->set(CurrencyContext::class, $this->autowire(CurrencyContextTest::class));

        $this->watchdog = $this->get(\KupShop\WatchdogBundle\Util\Watchdog::class);
        $this->mailArchive = $this->get(MailArchive::class);
        $this->mailArchive->clearArchive();
    }

    /** @dataProvider data_testPriceWatchdogIsSent */
    public function testPriceWatchdogIsSent(array $watchdog, bool $expectIsSent, ?string $expectInText = null): void
    {
        $this->createWatchdog(...$watchdog);

        $this->watchdog->generateWatchdogs();

        $mail = $this->mailArchive->getLast();

        if ($expectIsSent) {
            $this->assertNotEmpty($mail);
            if ($expectInText) {
                $this->assertContains($expectInText, $mail['body']);
            }
        } else {
            $this->assertEmpty($mail);
        }
    }

    private function data_testPriceWatchdogIsSent(): iterable
    {
        yield 'Watchdog s vyssi cenou, nez je aktualni cena CZK' => [[1, 1, 4, null, 5500, 'CZK'], true, 'pod 5 500 Kč'];
        yield 'Watchdog s vyssi cenou, nez je aktualni cena CZK' => [[1, 1, 4, 1, 5500, 'CZK'], true, 'pod 5 500 Kč'];
        yield 'Watchdog s nizsi cenou nez je aktualni cena CZK' => [[1, 1, 4, 1, 3500, 'CZK'], false];

        yield 'Watchdog s vyssi cenou v EUR, ktery byl ale ulozeny pod CZ sitou, takze se posle v CZK' => [[1, 1, 4, 1, 220, 'EUR'], true, 'pod 5 720 Kč'];
        yield 'Watchdog s nizsi cenou, nez je aktualni cena v EUR' => [[1, 1, 4, 1, 100, 'EUR'], false];

        yield 'Watchdog s vyssi cenou v EUR, ktery byl ale ulozeny pod SK sitou' => [[2, 1, 4, 1, 220, 'EUR'], true, 'pod 220,00 €'];
    }

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

    private function createWatchdog(int $siteId, int $userId, int $productId, ?int $variationId, float $price, string $currency): void
    {
        sqlQueryBuilder()
            ->insert('products_watchdog')
            ->directValues([
                'id_site' => $siteId,
                'id_user' => $userId,
                'id_product' => $productId,
                'id_variation' => $variationId,
                'price' => $price,
                'currency' => $currency,
                'availability' => 0,
            ])
            ->execute();
    }
}

class CurrencyContextTest extends \KupShop\I18nBundle\Context\CurrencyContext
{
    protected function loadActive()
    {
        return match (Contexts::get(LanguageContext::class)->getActiveId()) {
            'cs' => 'CZK',
            'sk' => 'EUR',
        };
    }
}
