<?php

namespace KupShop\OrderingBundle\Tests;

use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;

class SuppliersTest extends \DatabaseTestCase
{
    use CartTestTrait;

    private $productId = 3;

    /**
     * @dataProvider data_testInStoreWithAllowNegativeInStoreModule
     */
    public function testInStoreWithAllowNegativeInStoreModule(bool $module, $firstResult, $secondResult)
    {
        if ($module) {
            $this->addModule(\Modules::PRODUCTS_SUPPLIERS, \Modules::SUB_ALLOW_NEGATIVE_IN_STORE);
        }

        $product = new \Product();
        $product->createFromDB($this->productId);
        $product->prepareDeliveryText();

        $this->assertEquals($firstResult, [
            'inStore' => $product['inStore'],
            'in_store_suppliers' => $product['in_store_suppliers'],
            'deliveryTimeText' => $product['deliveryTimeText'],
            'deliveryTime' => $product['deliveryTime'],
        ]);

        $this->createCart();
        $this->insertProduct($product->id, null, 24);
        $this->cart->createFromDB();
        $return = $this->cart->productList->current();

        $this->assertEquals($secondResult, [
            'inStore' => $return['inStore'],
            'in_store_suppliers' => $return['in_store_suppliers'],
            'deliveryTimeText' => $return['deliveryTimeText'],
            'deliveryTime' => $return['deliveryTime'],
        ]);
    }

    public function data_testInStoreWithAllowNegativeInStoreModule()
    {
        yield 'WithAllowNegativeInStoreModule' => [true, [
            'inStore' => 0,
            'in_store_suppliers' => 29,
            'deliveryTimeText' => 'na cestě',
            'deliveryTime' => -1,
        ], [
            'inStore' => 0,
            'in_store_suppliers' => 30,
            'deliveryTimeText' => 'na cestě',
            'deliveryTime' => -1,
        ]];

        yield 'WithoutAllowNegativeInStoreModule' => [false, [
            'inStore' => 0,
            'in_store_suppliers' => 30,
            'deliveryTimeText' => 'na cestě',
            'deliveryTime' => -1,
        ], [
            'inStore' => 0,
            'in_store_suppliers' => 30,
            'deliveryTimeText' => 'na cestě',
            'deliveryTime' => -1,
        ]];
    }

    public function testSuppliersInStore()
    {
        $productId = 11;
        $variationId = 20;
        $product = new \Product();
        $product->createFromDB($productId);
        $product->getInStoreSuppliers();

        $variationSupplierInStore = $product->getInStoreSuppliers($variationId);
        $this->assertEquals('6', $variationSupplierInStore);

        $productSupplierInStore = $product->getInStoreSuppliers();
        $this->assertEquals('22', $productSupplierInStore);
    }

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