<?php

declare(strict_types=1);

namespace KupShop\OrderingBundle\Tests;

use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;

class OrderingTest extends \DatabaseTestCase
{
    use CartTestTrait;

    /** @dataProvider data_testHasOnlyVirtualProducts */
    public function testHasOnlyVirtualProducts(int $productId, array $actionHandlersData, bool $expectHasOnlyVirtualProducts): void
    {
        $this->createCart();
        $this->insertProduct($productId);

        // add action data (gifts)
        if (!empty($actionHandlersData)) {
            $this->cart->setActionHandlersData($actionHandlersData);
        }

        $this->recalculateCart();

        $this->assertEquals($expectHasOnlyVirtualProducts, $this->cart->hasOnlyVirtualProducts());

        // get all available virtual deliveries
        $virtualDeliveries = array_filter($this->cart->getDeliveryTypes(), fn ($x) => $x->getDelivery() instanceof \VirtualDelivery);

        if ($expectHasOnlyVirtualProducts) {
            $this->assertNotEmpty($virtualDeliveries);
        } else {
            $this->assertEmpty($virtualDeliveries);
        }
    }

    private function data_testHasOnlyVirtualProducts(): iterable
    {
        yield 'Virtual product in cart. Has only virtual products should be true.' => [2, [], true];
        yield 'Standard product in cart. Has only virtual products should be false' => [1, [], false];

        yield 'Standard product in cart with product gift. Has only virtual products should be false' => [1, [
            1 => ['gift' => 3],
        ], false];

        yield 'Virtual product in cart with product gift. Has only virtual products should be false' => [2, [
            1 => ['gift' => 3],
        ], false];

        yield 'Virtual product in cart with virtual product as gift. Has only virtual products should be true' => [2, [
            2 => ['gift' => 2],
        ], true];
    }

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