<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\EventListener;

use DH\Auditor\Provider\Doctrine\DoctrineProvider;
use DH\Auditor\Provider\Doctrine\Persistence\Schema\SchemaManager;
use DH\Auditor\Provider\Doctrine\Service\AuditingService;
use DH\Auditor\Provider\Doctrine\Service\StorageService;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;

final readonly class AuditorCreateSchemaListener
{
    public function __construct(private DoctrineProvider $provider)
    {
    }

    public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs): void
    {
        $metadata = $eventArgs->getClassMetadata();

        if (!\in_array($metadata->inheritanceType, [
            ClassMetadata::INHERITANCE_TYPE_NONE,
            ClassMetadata::INHERITANCE_TYPE_JOINED,
            ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE,
        ], true)) {
            throw new \Exception(\sprintf('Inheritance type "%s" is not yet supported', $metadata->inheritanceType));
        }

        $targetEntity = $metadata->name;
        if (!$this->provider->isAudited($metadata->name)) {
            $audited = false;
            if (
                $metadata->rootEntityName === $metadata->name
                && ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE === $metadata->inheritanceType
            ) {
                foreach ($metadata->subClasses as $subClass) {
                    if ($this->provider->isAudited($subClass)) {
                        $audited = true;
                        $targetEntity = $subClass;

                        break;
                    }
                }
            }

            if (!$audited) {
                return;
            }
        }

        $auditingServices = $this->provider->getAuditingServices();
        $storageServices = $this->provider->getStorageServices();

        \assert(array_values($auditingServices)[0] instanceof AuditingService);
        \assert(array_values($storageServices)[0] instanceof StorageService);
        $isSameEntityManager = 1 === \count($auditingServices) && 1 === \count($storageServices)
            && array_values($auditingServices)[0]->getEntityManager() === array_values($storageServices)[0]->getEntityManager();

        $updater = new SchemaManager($this->provider);
        $updater->createAuditTable($targetEntity, $isSameEntityManager ? $eventArgs->getSchema() : null);
    }
}
