<?php

declare(strict_types=1);

namespace KupShop\GraphQLBundle\Tests\ApiPublic;

use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
use KupShop\GraphQLBundle\Tests\GraphQLTestCase;

class CartTest extends GraphQLTestCase
{
    use CartTestTrait;

    protected function setUp(): void
    {
        parent::setUp();
        $this->addModule(\Modules::JS_SHOP);
    }

    /** @dataProvider data_purchaseStateProvider */
    public function testGetCart($applyModule): void
    {
        $applyModule();

        $this->prepareCart();
        $this->insertProduct(1, 16);

        $response = $this->doRequest(
            $this->getCartQuery()
        );

        $result = json_decode($response->getContent(), true);

        $this->assertEquals([
            'data' => [
                'cart' => [
                    'url' => '/kosik/',
                    'items' => [
                        [
                            'id' => $result['data']['cart']['items'][0]['id'], // ID v kartu je pokazdy jiny
                            'productId' => 1,
                            'variationId' => 16,
                            'name' => 'NIKE Capri LACE Test Dlouheho Nazvu Produktu Test Dlouheho Produktu Tes (Velikost: 45)',
                            'pieces' => 1,
                            'step' => 1,
                            'price' => [
                                'withVat' => 800,
                                'withoutVat' => 661.157,
                                'vatValue' => 138.843,
                                'vat' => 21,
                                'currency' => [
                                    'code' => 'CZK',
                                    'name' => 'Česká koruna',
                                    'symbol' => 'Kč',
                                ],
                            ],
                        ],
                    ],
                    'totalPrice' => [
                        'withVat' => 800,
                        'withoutVat' => 661.157,
                        'currency' => [
                            'code' => 'CZK',
                            'name' => 'Česká koruna',
                            'symbol' => 'Kč',
                        ],
                    ],
                ],
            ],
        ], $result);
    }

    /** @dataProvider data_purchaseStateProvider */
    public function testGetCartEmpty($applyModule): void
    {
        $applyModule();

        $response = $this->doRequest(
            $this->getCartQuery()
        );

        $result = json_decode($response->getContent(), true);

        $this->assertEquals([
            'data' => [
                'cart' => [
                    'url' => '/kosik/',
                    'items' => [],
                    'totalPrice' => [
                        'withVat' => 0,
                        'withoutVat' => 0,
                        'currency' => [
                            'code' => 'CZK',
                            'name' => 'Česká koruna',
                            'symbol' => 'Kč',
                        ],
                    ],
                ],
            ],
        ], $result);
    }

    private function getCartQuery(): string
    {
        return '
        query {
          cart {
            url
            items {
              id
              productId
              variationId
              name
              pieces
              step
              price: piecePrice {
                withVat
                withoutVat
                vatValue
                vat
                currency {
                  code
                  name
                  symbol
                }
              }
            }
            totalPrice {
              withVat
              withoutVat
              currency {
                code
                name
                symbol
              }
            }
          }
        }
        ';
    }
}
