<?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\Delete\Tests;

use BaksDev\Products\Sign\Controller\Admin\Tests\DeleteControllerTest;
use BaksDev\Products\Sign\Entity\Event\ProductSignEvent;
use BaksDev\Products\Sign\Entity\ProductSign;
use BaksDev\Products\Sign\Repository\CurrentEvent\ProductSignCurrentEventInterface;
use BaksDev\Products\Sign\Type\Id\ProductSignUid;
use BaksDev\Products\Sign\Type\Status\ProductSignStatus\Collection\ProductSignStatusCollection;
use BaksDev\Products\Sign\UseCase\Admin\Delete\ProductSignDeleteDTO;
use BaksDev\Products\Sign\UseCase\Admin\Delete\ProductSignDeleteHandler;
use BaksDev\Products\Sign\UseCase\Admin\New\ProductSignDTO;
use BaksDev\Products\Sign\UseCase\Admin\Cancel\ProductSignCancelDTO;
use BaksDev\Products\Sign\UseCase\Admin\Cancel\Tests\ProductSignCancelHandleTest;
use BaksDev\Users\Profile\UserProfile\Type\Id\UserProfileUid;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\Attribute\When;

/**
 * @group @group products-sign
 *
 * @depends BaksDev\Products\Sign\UseCase\Admin\Cancel\Tests\ProductSignCancelHandleTest::class
 * @depends BaksDev\Products\Sign\Controller\Admin\Tests\DeleteControllerTest::class
 *
 */
#[When(env: 'test')]
final class ProductSignDeleteHandleTest extends KernelTestCase
{
    public function testUseCase(): void
    {
        /**
         * Инициируем статус для итератора тегов
         * @var ProductSignStatusCollection $status
         */
        $status = self::getContainer()->get(ProductSignStatusCollection::class);
        $status->cases();

        /** @var ProductSignCurrentEventInterface $ProductSignCurrentEvent */
        $ProductSignCurrentEvent = self::getContainer()->get(ProductSignCurrentEventInterface::class);
        $ProductSignEvent = $ProductSignCurrentEvent->findByProductSign(ProductSignUid::TEST);
        self::assertNotNull($ProductSignEvent);


        /** @see ProductSignDTO */

        $ProductSignDTO = new ProductSignDeleteDTO($UserProfileUid = clone new UserProfileUid());
        $ProductSignEvent->getDto($ProductSignDTO);
        self::assertSame($UserProfileUid, $ProductSignDTO->getProfile());


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

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

    }

    public static function tearDownAfterClass(): void
    {
        /** @var EntityManagerInterface $em */
        $em = self::getContainer()->get(EntityManagerInterface::class);

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

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

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

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

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

        self::assertTrue(true);
    }
}