<?php

declare(strict_types=1);

namespace App\EventSubscriber;

use App\Entity\Composant;
use App\Entity\CustomFieldValue;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Events;

#[AsDoctrineListener(event: Events::onFlush)]
final class ComposantAuditSubscriber extends AbstractAuditSubscriber
{
    protected function supports(object $entity): bool
    {
        return $entity instanceof Composant;
    }

    protected function entityType(): string
    {
        return 'composant';
    }

    protected function hasCollectionTracking(): bool
    {
        return true;
    }

    protected function getOwnerFromCustomFieldValue(CustomFieldValue $cfv): ?object
    {
        return $cfv->getComposant();
    }

    protected function snapshotEntity(object $entity): array
    {
        $pieceSlots = [];
        foreach ($entity->getPieceSlots() as $slot) {
            $pieceSlots[] = [
                'id'              => $slot->getId(),
                'typePieceId'     => $slot->getTypePiece()?->getId(),
                'selectedPieceId' => $slot->getSelectedPiece()?->getId(),
                'quantity'        => $slot->getQuantity(),
                'position'        => $slot->getPosition(),
            ];
        }

        $subcomponentSlots = [];
        foreach ($entity->getSubcomponentSlots() as $slot) {
            $subcomponentSlots[] = [
                'id'                  => $slot->getId(),
                'alias'               => $slot->getAlias(),
                'familyCode'          => $slot->getFamilyCode(),
                'typeComposantId'     => $slot->getTypeComposant()?->getId(),
                'selectedComposantId' => $slot->getSelectedComposant()?->getId(),
                'position'            => $slot->getPosition(),
            ];
        }

        $productSlots = [];
        foreach ($entity->getProductSlots() as $slot) {
            $productSlots[] = [
                'id'                => $slot->getId(),
                'typeProductId'     => $slot->getTypeProduct()?->getId(),
                'selectedProductId' => $slot->getSelectedProduct()?->getId(),
                'familyCode'        => $slot->getFamilyCode(),
                'position'          => $slot->getPosition(),
            ];
        }

        $customFieldValues = [];
        foreach ($entity->getCustomFieldValues() as $cfv) {
            $customFieldValues[] = [
                'id'        => $cfv->getId(),
                'fieldName' => $cfv->getCustomField()?->getName(),
                'fieldId'   => $cfv->getCustomField()?->getId(),
                'value'     => $cfv->getValue(),
            ];
        }

        return [
            'id'              => $entity->getId(),
            'name'            => $this->safeGet($entity, 'getName'),
            'reference'       => $this->safeGet($entity, 'getReference'),
            'description'     => $this->safeGet($entity, 'getDescription'),
            'prix'            => $this->safeGet($entity, 'getPrix'),
            'typeComposant'   => $this->normalizeValue($this->safeGet($entity, 'getTypeComposant')),
            'product'         => $this->normalizeValue($this->safeGet($entity, 'getProduct')),
            'constructeurIds' => array_map(
                fn ($link) => [
                    'id'                => $link->getConstructeur()->getId(),
                    'name'              => $link->getConstructeur()->getName(),
                    'supplierReference' => $link->getSupplierReference(),
                ],
                $entity->getConstructeurLinks()->toArray(),
            ),
            'pieceSlots'        => $pieceSlots,
            'subcomponentSlots' => $subcomponentSlots,
            'productSlots'      => $productSlots,
            'customFieldValues' => $customFieldValues,
            'version'           => $this->safeGet($entity, 'getVersion'),
        ];
    }
}
