<?php

namespace App\Tests\Controller\Admin;

use App\Controller\Admin\TarificationController;
use App\Entity\PriceAutomatic;
use App\Repository\PriceAutomaticRepository;
use App\Service\MeilisearchService;
use App\Service\StripePriceService;
use App\Service\TarificationService;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Twig\Environment;

class TarificationControllerTest extends TestCase
{
    private function createContainer(): ContainerInterface
    {
        $session = new Session(new MockArraySessionStorage());
        $stack = $this->createStub(RequestStack::class);
        $stack->method('getSession')->willReturn($session);

        $twig = $this->createStub(Environment::class);
        $twig->method('render')->willReturn('<html></html>');

        $router = $this->createStub(RouterInterface::class);
        $router->method('generate')->willReturn('/admin/tarification');

        $container = $this->createStub(ContainerInterface::class);
        $container->method('has')->willReturn(true);
        $container->method('get')->willReturnMap([
            ['twig', $twig],
            ['router', $router],
            ['security.authorization_checker', $this->createStub(AuthorizationCheckerInterface::class)],
            ['security.token_storage', $this->createStub(TokenStorageInterface::class)],
            ['request_stack', $stack],
            ['parameter_bag', $this->createStub(\Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface::class)],
        ]);

        return $container;
    }

    private function createPrice(): PriceAutomatic
    {
        $p = new PriceAutomatic();
        $p->setType('esyweb_business');
        $p->setTitle('Esy-Web Business');
        $p->setPriceHt('500.00');
        $p->setMonthPrice('100.00');
        $p->setPeriod(1);

        return $p;
    }

    public function testIndexNoCreated(): void
    {
        $tarification = $this->createStub(TarificationService::class);
        $tarification->method('ensureDefaultPrices')->willReturn([]);
        $tarification->method('getAll')->willReturn([]);

        $controller = new TarificationController();
        $controller->setContainer($this->createContainer());

        $response = $controller->index($tarification);
        $this->assertSame(200, $response->getStatusCode());
    }

    public function testIndexWithCreated(): void
    {
        $tarification = $this->createStub(TarificationService::class);
        $tarification->method('ensureDefaultPrices')->willReturn(['esyweb_business', 'esymail']);
        $tarification->method('getAll')->willReturn([$this->createPrice()]);

        $controller = new TarificationController();
        $controller->setContainer($this->createContainer());

        $response = $controller->index($tarification);
        $this->assertSame(200, $response->getStatusCode());
    }

    public function testEditNotFound(): void
    {
        $repo = $this->createStub(PriceAutomaticRepository::class);
        $repo->method('find')->willReturn(null);

        $controller = new TarificationController();
        $controller->setContainer($this->createContainer());

        $this->expectException(NotFoundHttpException::class);
        $controller->edit(999, new Request(), $repo, $this->createStub(EntityManagerInterface::class), $this->createStub(StripePriceService::class), $this->createStub(MeilisearchService::class));
    }

    public function testEditSuccessStripeOk(): void
    {
        $price = $this->createPrice();

        $repo = $this->createStub(PriceAutomaticRepository::class);
        $repo->method('find')->willReturn($price);

        $request = new Request([], [
            'title' => 'Updated Title',
            'description' => 'New desc',
            'priceHt' => '600.00',
            'monthPrice' => '120.00',
            'period' => '3',
            'stripeId' => 'price_abc',
            'stripeAbonnementId' => 'sub_xyz',
        ]);
        $request->setMethod('POST');

        $controller = new TarificationController();
        $controller->setContainer($this->createContainer());

        $response = $controller->edit(1, $request, $repo, $this->createStub(EntityManagerInterface::class), $this->createStub(StripePriceService::class), $this->createStub(MeilisearchService::class));
        $this->assertSame(302, $response->getStatusCode());
        $this->assertSame('Updated Title', $price->getTitle());
        $this->assertSame('600.00', $price->getPriceHt());
    }

    public function testEditStripeError(): void
    {
        $price = $this->createPrice();

        $repo = $this->createStub(PriceAutomaticRepository::class);
        $repo->method('find')->willReturn($price);

        $stripe = $this->createStub(StripePriceService::class);
        $stripe->method('syncPrice')->willThrowException(new \RuntimeException('Stripe down'));

        $request = new Request([], [
            'title' => 'T', 'description' => '', 'priceHt' => '1.00',
            'monthPrice' => '0.00', 'period' => '1', 'stripeId' => '', 'stripeAbonnementId' => '',
        ]);
        $request->setMethod('POST');

        $controller = new TarificationController();
        $controller->setContainer($this->createContainer());

        $response = $controller->edit(1, $request, $repo, $this->createStub(EntityManagerInterface::class), $stripe, $this->createStub(MeilisearchService::class));
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testEditMeilisearchError(): void
    {
        $price = $this->createPrice();

        $repo = $this->createStub(PriceAutomaticRepository::class);
        $repo->method('find')->willReturn($price);

        $meilisearch = $this->createStub(MeilisearchService::class);
        $meilisearch->method('indexPrice')->willThrowException(new \RuntimeException('Meili down'));

        $request = new Request([], [
            'title' => 'T', 'description' => '', 'priceHt' => '1.00',
            'monthPrice' => '0.00', 'period' => '1', 'stripeId' => '', 'stripeAbonnementId' => '',
        ]);
        $request->setMethod('POST');

        $controller = new TarificationController();
        $controller->setContainer($this->createContainer());

        $response = $controller->edit(1, $request, $repo, $this->createStub(EntityManagerInterface::class), $this->createStub(StripePriceService::class), $meilisearch);
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testPurgeWithNoPrices(): void
    {
        $repo = $this->createStub(PriceAutomaticRepository::class);
        $repo->method('findAll')->willReturn([]);

        $em = $this->createMock(EntityManagerInterface::class);
        $em->expects($this->once())->method('flush');

        $meilisearch = $this->createStub(MeilisearchService::class);

        $controller = new TarificationController();
        $controller->setContainer($this->createContainer());

        $response = $controller->purge($repo, $em, $meilisearch, '');
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testPurgeWithPrices(): void
    {
        $price = $this->createPrice();

        $repo = $this->createStub(PriceAutomaticRepository::class);
        $repo->method('findAll')->willReturn([$price]);

        $em = $this->createMock(EntityManagerInterface::class);
        $em->expects($this->once())->method('remove')->with($price);
        $em->expects($this->once())->method('flush');

        $meilisearch = $this->createStub(MeilisearchService::class);

        $controller = new TarificationController();
        $controller->setContainer($this->createContainer());

        $response = $controller->purge($repo, $em, $meilisearch, '');
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testPurgeMeilisearchError(): void
    {
        $price = $this->createPrice();

        $repo = $this->createStub(PriceAutomaticRepository::class);
        $repo->method('findAll')->willReturn([$price]);

        $em = $this->createStub(EntityManagerInterface::class);

        $meilisearch = $this->createStub(MeilisearchService::class);
        $meilisearch->method('removePrice')->willThrowException(new \RuntimeException('Meili error'));

        $controller = new TarificationController();
        $controller->setContainer($this->createContainer());

        // Should not throw, error is swallowed
        $response = $controller->purge($repo, $em, $meilisearch, '');
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testPurgeWithStripeKeyAndPriceHasStripeId(): void
    {
        $price = $this->createPrice();
        $price->setStripeId('prod_abc123');

        $repo = $this->createStub(PriceAutomaticRepository::class);
        $repo->method('findAll')->willReturn([$price]);

        $em = $this->createMock(EntityManagerInterface::class);
        $em->expects($this->once())->method('remove')->with($price);
        $em->expects($this->once())->method('flush');

        $meilisearch = $this->createStub(MeilisearchService::class);

        $controller = new TarificationController();
        $controller->setContainer($this->createContainer());

        // Pass a non-empty stripeSk to trigger the Stripe deactivation branch.
        // The Stripe API call will fail (no real credentials), but the error is swallowed.
        $response = $controller->purge($repo, $em, $meilisearch, 'sk_test_dummy');
        $this->assertSame(302, $response->getStatusCode());
    }
}
