<?php

namespace App\Tests\Security;

use App\Entity\Formules;
use App\Entity\Product;
use App\Repository\FormulesRepository;
use App\Repository\OptionsRepository;
use App\Repository\ProductRepository;
use App\Security\SiteMapListener;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\TestCase;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Service\UrlContainerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Vich\UploaderBundle\Storage\StorageInterface;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;

#[AllowMockObjectsWithoutExpectations]
class SiteMapListenerTest extends TestCase
{
    private $uploaderHelper;
    private $storage;
    private $optionsRepository;
    private $productRepository;
    private $formulesRepository;
    private $entityManager;
    private $listener;

    protected function setUp(): void
    {
        $this->storage = $this->createMock(StorageInterface::class);
        $this->uploaderHelper = new UploaderHelper($this->storage); // Instantiate real class
        
        $this->optionsRepository = $this->createMock(OptionsRepository::class);
        $this->productRepository = $this->createMock(ProductRepository::class);
        $this->formulesRepository = $this->createMock(FormulesRepository::class);
        $this->entityManager = $this->createMock(EntityManagerInterface::class);

        $this->listener = new SiteMapListener(
            $this->uploaderHelper,
            $this->optionsRepository,
            $this->productRepository,
            $this->formulesRepository,
            $this->entityManager
        );
    }

    public function testPopulate()
    {
        $urlContainer = $this->createMock(UrlContainerInterface::class);
        $urlGenerator = $this->createMock(UrlGeneratorInterface::class);
        $event = new SitemapPopulateEvent($urlContainer, $urlGenerator);

        $urlGenerator->method('generate')->willReturn('https://example.com/page');

        // Mock Data
        $formule = $this->createMock(Formules::class);
        $formule->method('slug')->willReturn('f-1');
        $formule->method('getUpdatedAt')->willReturn(new \DateTimeImmutable());
        $this->formulesRepository->method('findBy')->willReturn([$formule]);
        
        // Mock UploaderHelper behavior via Storage
        $this->storage->method('resolveUri')->willReturn('/images/formule.jpg');

        $product = $this->createMock(Product::class);
        $product->method('slug')->willReturn('p-1');
        $product->method('getUpdatedAt')->willReturn(new \DateTimeImmutable());
        $product->method('getName')->willReturn('Prod 1');
        $this->productRepository->method('findAll')->willReturn([$product]);

        $this->optionsRepository->method('findAll')->willReturn([]);

        $urlContainer->expects($this->atLeastOnce())->method('addUrl');

        $this->listener->populate($event);
    }
}
