<?php

declare(strict_types=1);

namespace App\EventSubscriber;

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

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

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

    protected function snapshotEntity(object $entity): array
    {
        return [
            'id'        => $entity->getId(),
            'name'      => $this->safeGet($entity, 'getName'),
            'filename'  => $this->safeGet($entity, 'getFilename'),
            'mimeType'  => $this->safeGet($entity, 'getMimeType'),
            'size'      => $this->safeGet($entity, 'getSize'),
            'machine'   => $this->normalizeValue($this->safeGet($entity, 'getMachine')),
            'composant' => $this->normalizeValue($this->safeGet($entity, 'getComposant')),
            'piece'     => $this->normalizeValue($this->safeGet($entity, 'getPiece')),
            'product'   => $this->normalizeValue($this->safeGet($entity, 'getProduct')),
            'site'      => $this->normalizeValue($this->safeGet($entity, 'getSite')),
        ];
    }
}
