<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Twig;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Modal\SimpleModalProductReviews;
use CpCreation\VitiCore\Product\Model\Product;
use PHPUnit\Framework\TestCase;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveListener;

final class ProductReviewsModalComponentTest extends TestCase
{
    public function testItOpensPaginatesAndClosesForAProduct(): void
    {
        $modal = new SimpleModalProductReviews();
        $product = new Product();

        $modal->openProductReviewsModal($product);

        self::assertTrue($modal->open);
        self::assertSame($product, $modal->product);
        self::assertSame(1, $modal->page);

        $modal->nextPage();
        self::assertSame(2, $modal->page);

        $modal->previousPage();
        $modal->previousPage();
        self::assertSame(1, $modal->page);

        $modal->close();
        self::assertFalse($modal->open);
        self::assertNull($modal->product);
        self::assertSame(1, $modal->page);
    }

    public function testItKeepsTheLiveHooksRequiredByProductCards(): void
    {
        $reflection = new \ReflectionClass(SimpleModalProductReviews::class);

        self::assertNotEmpty(
            $reflection->getMethod('openProductReviewsModal')->getAttributes(LiveListener::class),
        );
        self::assertNotEmpty($reflection->getMethod('previousPage')->getAttributes(LiveAction::class));
        self::assertNotEmpty($reflection->getMethod('nextPage')->getAttributes(LiveAction::class));
        self::assertNotEmpty($reflection->getMethod('close')->getAttributes(LiveAction::class));
    }
}
