<?php

declare(strict_types=1);

namespace App\Module\Directory\Infrastructure\EventListener;

use App\Module\Directory\Domain\Entity\ReportDocument;
use Doctrine\ORM\Event\PreRemoveEventArgs;
use Psr\Log\LoggerInterface;

final readonly class ReportDocumentListener
{
    public function __construct(
        private string $uploadDir,
        private LoggerInterface $logger,
    ) {}

    public function preRemove(ReportDocument $document, PreRemoveEventArgs $args): void
    {
        $fileName = $document->getFileName();

        if (null === $fileName) {
            return;
        }

        $path = $this->uploadDir.'/'.$fileName;

        if (is_file($path) && !@unlink($path)) {
            $this->logger->warning('Failed to delete report document file', ['path' => $path]);
        }
    }
}
