<?php

declare(strict_types=1);

namespace DH\Auditor\Tests\Provider\Doctrine\Issues;

use DH\Auditor\Model\Transaction;
use DH\Auditor\Provider\Doctrine\DoctrineProvider;
use DH\Auditor\Provider\Doctrine\Service\AuditingService;
use DH\Auditor\Provider\Doctrine\Service\StorageService;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue40\CoreCase;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue40\DieselCase;
use DH\Auditor\Tests\Provider\Doctrine\Traits\ReaderTrait;
use DH\Auditor\Tests\Provider\Doctrine\Traits\Schema\SchemaSetupTrait;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;

/**
 * @internal
 */
#[Small]
final class Issue40Test extends TestCase
{
    use ReaderTrait;
    use SchemaSetupTrait;

    public function testIssue40(): void
    {
        $storageServices = [
            CoreCase::class => $this->provider->getStorageServiceForEntity(CoreCase::class),
            DieselCase::class => $this->provider->getStorageServiceForEntity(DieselCase::class),
        ];

        $reader = $this->createReader();

        $coreCase = new CoreCase();
        $coreCase->type = 'type1';
        $coreCase->status = 'status1';
        $storageServices[CoreCase::class]->getEntityManager()->persist($coreCase);
        $this->flushAll($storageServices);

        $dieselCase = new DieselCase();
        $dieselCase->coreCase = $coreCase;
        $dieselCase->setName('yo');
        $storageServices[DieselCase::class]->getEntityManager()->persist($dieselCase);
        $this->flushAll($storageServices);

        $audits = $reader->createQuery(CoreCase::class)->execute();
        $this->assertCount(1, $audits, 'results count ok.');
        $this->assertSame(Transaction::INSERT, $audits[0]->getType(), 'Reader::INSERT operation.');

        $audits = $reader->createQuery(DieselCase::class)->execute();
        $this->assertCount(1, $audits, 'results count ok.');
        $this->assertSame(Transaction::INSERT, $audits[0]->getType(), 'Reader::INSERT operation.');
    }

    private function createAndInitDoctrineProvider(): void
    {
        $auditor = $this->createAuditor();
        $this->provider = new DoctrineProvider($this->createProviderConfiguration());

        $entityManager = $this->createEntityManager([
            __DIR__.'/../../../../src/Provider/Doctrine/Auditing/Annotation',
            __DIR__.'/../Fixtures/Issue40',
        ]);
        $this->provider->registerStorageService(new StorageService('default', $entityManager));
        $this->provider->registerAuditingService(new AuditingService('default', $entityManager));

        $auditor->registerProvider($this->provider);
    }

    private function configureEntities(): void
    {
        $this->provider->getConfiguration()->setEntities([
            CoreCase::class => ['enabled' => true],
            DieselCase::class => ['enabled' => true],
        ]);
    }
}
