<?php

use KupShop\CatalogBundle\Parameters\ParameterFinder;
use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;

class ParametersTest extends \DatabaseTestCase
{
    use CartTestTrait;

    public function testFetchParameters()
    {
        $params = [
            14 => [
                'name' => 'Barva',
                'unit' => '',
                'value' => 'červená',
                'values' => [
                    9 => [
                        'id' => '9',
                        'value' => 'červená',
                        'descr' => '',
                    ],
                ],
                'descr' => '',
                'value_type' => 'list',
                'value_id' => '9',
            ],
            17 => [
                'name' => 'Typ hlavy',
                'unit' => '',
                'value' => 'Bez příklepu',
                'values' => [
                    14 => [
                        'id' => '14',
                        'value' => 'Bez příklepu',
                        'descr' => '',
                    ],
                ],
                'descr' => '<p>Vrtačka s příklepem. Vratačka bez příklepu</p>',
                'value_type' => 'list',
                'value_id' => '14',
            ],
        ];

        $product = new Product();
        $product->createFromDB(10);
        $this->assertArraySubset($params, $product->fetchParameters());

        $product = new Product();
        $product->createFromDB(10);
        $this->assertCount(1, $product->fetchParameters([17]));

        // Set parameter 14 as configurable
        $product = new Product();
        $product->createFromDB(10);
        ParameterConfiguration::insertValue(14, 10);

        $this->assertArraySubset([17 => $params[17]], $product->fetchParameters());
        $this->assertCount(1, $product->fetchParameters());
        $this->assertCount(1, $product->configurations);
        $this->assertInstanceOf(ParameterConfiguration::class, reset($product->configurations));
        $this->assertEquals(14, reset($product->configurations)->id);
    }

    public function testOrderWithConfiguration()
    {
        ParameterConfiguration::insertValue(14, 10);

        $this->createCart();
        $cart = $this->cart;

        $item = [
            'id_product' => 10,
            'id_variation' => null,
            'pieces' => 1,
            'note' => [
                'configurations' => [
                    '14' => 16,
                ],
            ],
        ];
        $cart->addItem($item);
        $cart->createFromDB();

        $this->assertEquals(round(2011.074 * 0.8 + 50) /* 20% sleva na produktu */ , $cart->totalPriceWithVat->asFloat());
    }

    /** @dataProvider data_testParameterFinderUpdateProductParameters */
    public function testParameterFinderUpdateProductParameters(int $productId, int $parameterId, array $values): void
    {
        $parameterFinder = $this->get(ParameterFinder::class);

        $parameterFinder->updateProductParameters($productId, $parameterId, $values);

        $parameter = $parameterFinder->getParameterById($parameterId);

        $currentValues = array_map(fn ($x) => $x['value'], sqlQueryBuilder()
            ->select('value_'.$parameter->value_type.' as value')
            ->from('parameters_products')
            ->where(\Query\Operator::equals(['id_product' => $productId, 'id_parameter' => $parameterId]))
            ->execute()->fetchAllAssociative());

        $this->assertEquals($values, $currentValues);
    }

    protected function data_testParameterFinderUpdateProductParameters(): array
    {
        return [
            // otestovat zmenu poctu hodnot
            [1, 14, [2]],
            // otestovat zmenu poctu hodnot a ze se spravne zmeni poradi
            [1, 14, [2, 13, 1]],
            // otestovat smazani hodnot
            [1, 14, []],
            // otestovat, ze hodnoty zustanou stejne, kdyz poslu stejne hodnoty
            [1, 14, [2, 1]],
        ];
    }

    protected function getDataSet()
    {
        return $this->getJsonDataSet(/* @lang JSON */
            '
          {
          "parameters_products": [
              {
                "id":16,
                "id_product":10,
                "id_parameter":14,
                "value_list":9,
                "value_char":null,
                "value_float":null,
                "unit":null,
                "weight":null,
                "configuration_price": 50
              },
              {
                "id":17,
                "id_product":10,
                "id_parameter":17,
                "value_list":14,
                "value_char":null,
                "value_float":null,
                "unit":null,
                "weight":null,
                "configuration_price":null
              },    
              {
                "id": 1,
                "id_product": 1,
                "id_parameter": 14,
                "value_list": 2,
                "value_char": null,
                "value_float": null,
                "unit": null,
                "weight": null,
                "configuration_price": null
              },
              {
                "id": 2,
                "id_product": 1,
                "id_parameter": 14,
                "value_list": 1,
                "value_char": null,
                "value_float": null,
                "unit": null,
                "weight": null,
                "configuration_price": null
              }
            ]
          }
        ');
    }
}
