<?php
/*
 *  Copyright 2023.  Baks.dev <admin@baks.dev>
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is furnished
 *  to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */

declare(strict_types=1);

namespace BaksDev\Products\Sign\UseCase\Admin\New\Tests;

use BaksDev\Core\Doctrine\DBALQueryBuilder;
use BaksDev\Products\Product\Type\Id\ProductUid;
use BaksDev\Products\Product\Type\Offers\ConstId\ProductOfferConst;
use BaksDev\Products\Product\Type\Offers\Variation\ConstId\ProductVariationConst;
use BaksDev\Products\Product\Type\Offers\Variation\Modification\ConstId\ProductModificationConst;
use BaksDev\Products\Sign\Entity\Event\ProductSignEvent;
use BaksDev\Products\Sign\Entity\ProductSign;
use BaksDev\Products\Sign\Type\Id\ProductSignUid;
use BaksDev\Products\Sign\Type\Status\ProductSignStatus\Collection\ProductSignStatusCollection;
use BaksDev\Products\Sign\Type\Status\ProductSignStatus\ProductSignStatusNew;
use BaksDev\Products\Sign\UseCase\Admin\New\Code\ProductSignCodeDTO;
use BaksDev\Products\Sign\UseCase\Admin\New\ProductSignDTO;
use BaksDev\Products\Sign\UseCase\Admin\New\ProductSignHandler;
use BaksDev\Users\Profile\UserProfile\Type\Id\UserProfileUid;
use BaksDev\Users\User\Type\Id\UserUid;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\Attribute\When;

/**
 * @group products-sign
 */
#[When(env: 'test')]
final class ProductSignNewHandleTest extends KernelTestCase
{
    public static function setUpBeforeClass(): void
    {
        /**
         * Инициируем статус для итератора тегов
         * @var ProductSignStatusCollection $WbSupplyStatus
         */
        $WbSupplyStatus = self::getContainer()->get(ProductSignStatusCollection::class);
        $WbSupplyStatus->cases();

        /** @var EntityManagerInterface $em */
        $em = self::getContainer()->get(EntityManagerInterface::class);

        $main = $em->getRepository(ProductSign::class)
            ->findOneBy(['id' => ProductSignUid::TEST]);

        if($main)
        {
            $em->remove($main);
        }

        /* WbBarcodeEvent */

        $event = $em->getRepository(ProductSignEvent::class)
            ->findBy(['main' => ProductSignUid::TEST]);

        foreach($event as $remove)
        {
            $em->remove($remove);
        }

        $em->flush();
        $em->clear();
    }


    public function testUseCase(): void
    {
        /** @see ProductSignDTO */

        $ProductSignDTO = new ProductSignDTO();
        $ProductSignDTO->setProfile($UserProfileUid = new UserProfileUid());
        self::assertSame($UserProfileUid, $ProductSignDTO->getProfile());
        self::assertTrue($ProductSignDTO->getStatus()->equals(ProductSignStatusNew::class));


        /** @see ProductSignCodeDTO */

        $ProductSignCodeDTO = $ProductSignDTO->getCode();

        $ProductSignCodeDTO->setUsr($UserUid = new UserUid());
        self::assertSame($UserUid, $ProductSignCodeDTO->getUsr());

        $ProductSignCodeDTO->setCode('code');
        self::assertEquals('code', $ProductSignCodeDTO->getCode());

        $ProductSignCodeDTO->setQr('qr');
        self::assertEquals('qr', $ProductSignCodeDTO->getQr());

        $ProductUid = new ProductUid();
        $ProductSignCodeDTO->setProduct($ProductUid);
        self::assertSame($ProductUid, $ProductSignCodeDTO->getProduct());

        $ProductOfferConst = new ProductOfferConst();
        $ProductSignCodeDTO->setOffer($ProductOfferConst);
        self::assertSame($ProductOfferConst, $ProductSignCodeDTO->getOffer());

        $ProductVariationConst = new ProductVariationConst();
        $ProductSignCodeDTO->setVariation($ProductVariationConst);
        self::assertSame($ProductVariationConst, $ProductSignCodeDTO->getVariation());

        $ProductModificationConst = new ProductModificationConst();
        $ProductSignCodeDTO->setModification($ProductModificationConst);
        self::assertSame($ProductModificationConst, $ProductSignCodeDTO->getModification());


        /** @var ProductSignHandler $ProductSignHandler */
        $ProductSignHandler = self::getContainer()->get(ProductSignHandler::class);
        $handle = $ProductSignHandler->handle($ProductSignDTO);

        self::assertTrue(($handle instanceof ProductSign), $handle.': Ошибка ProductSign');

    }

    public function testComplete(): void
    {

        /** @var DBALQueryBuilder $dbal */
        $dbal = self::getContainer()->get(DBALQueryBuilder::class);

        $dbal->createQueryBuilder(self::class);

        $dbal->from(ProductSign::class)
            ->where('id = :id')
            ->setParameter('id', ProductSignUid::TEST);

        self::assertTrue($dbal->fetchExist());

    }
}