<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Cart;

use CpCreation\VitiCore\Cart\Model\WithdrawalRequest;
use CpCreation\VitiCore\Cart\Repository\WithdrawalRequestRepository;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Doctrine\UuidType;

final class WithdrawalRequestRepositoryTest extends TestCase
{
    public function testAdminQueryFiltersPendingRequests(): void
    {
        $queryBuilder = $this->createRepository()->createAdminQueryBuilder(status: 'pending');

        self::assertStringContainsString(
            'withdrawal_request.processedAt IS NULL',
            $queryBuilder->getDQL(),
        );
    }

    public function testAdminQueryFiltersProcessedRequests(): void
    {
        $queryBuilder = $this->createRepository()->createAdminQueryBuilder(status: 'processed');

        self::assertStringContainsString(
            'withdrawal_request.processedAt IS NOT NULL',
            $queryBuilder->getDQL(),
        );
    }

    private function createRepository(): WithdrawalRequestRepository
    {
        if (!Type::hasType('uuid')) {
            Type::addType('uuid', UuidType::class);
        }

        $configuration = ORMSetup::createAttributeMetadataConfiguration([
            \dirname(__DIR__, 2) . '/src/Cart/Model',
        ], true);
        $connection = DriverManager::getConnection([
            'driver' => 'pdo_sqlite',
            'memory' => true,
        ], $configuration);
        $entityManager = new EntityManager($connection, $configuration);
        $registry = $this->createMock(ManagerRegistry::class);
        $registry->method('getManagerForClass')
            ->with(WithdrawalRequest::class)
            ->willReturn($entityManager);

        return new WithdrawalRequestRepository($registry);
    }
}
