<?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
    {
        return [
            'id'              => $entity->getId(),
            'name'            => $this->safeGet($entity, 'getName'),
            'reference'       => $this->safeGet($entity, 'getReference'),
            'prix'            => $this->safeGet($entity, 'getPrix'),
            'typeComposant'   => $this->normalizeValue($this->safeGet($entity, 'getTypeComposant')),
            'product'         => $this->normalizeValue($this->safeGet($entity, 'getProduct')),
            'constructeurIds' => $this->normalizeCollection($entity->getConstructeurs()),
        ];
    }
}
