<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Modal;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Behavior\HasTemplateOverride;
use CpCreation\VitiCore\Product\Model\Product;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveListener;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent('modal:SimpleModalProductReviews', template: '@components/modal/SimpleModalProductReviews.html.twig')]
final class SimpleModalProductReviews
{
    use DefaultActionTrait;
    use HasTemplateOverride;

    public string $id = 'product-reviews-modal';

    #[LiveProp]
    public bool $open = false;

    #[LiveProp]
    public ?Product $product = null;

    #[LiveProp]
    public int $page = 1;

    #[LiveListener('productReviewsModal:open')]
    public function openProductReviewsModal(
        #[LiveArg('productId')]
        Product $product,
    ): void {
        $this->product = $product;
        $this->page = 1;
        $this->open = true;
    }

    #[LiveAction]
    public function previousPage(): void
    {
        $this->page = max(1, $this->page - 1);
    }

    #[LiveAction]
    public function nextPage(): void
    {
        ++$this->page;
    }

    #[LiveAction]
    public function close(): void
    {
        $this->open = false;
        $this->product = null;
        $this->page = 1;
    }
}
