<?php

namespace KupShop\CatalogBundle\View;

use KupShop\CatalogBundle\Util\ReviewsUtil;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Views\View;

class ReviewsListView extends View
{
    protected $template = 'reviews/reviews.list.tpl';

    private int $page = 1;

    public function __construct(private readonly ReviewsUtil $reviewsUtil)
    {
    }

    public function setPage(int $page): self
    {
        $this->page = $page;

        return $this;
    }

    public function getTitle()
    {
        return translate('reviewsList', 'reviews');
    }

    public function getBodyVariables(): array
    {
        $vars = parent::getBodyVariables();

        $pager = $this->getPager();

        $languageContext = Contexts::get(LanguageContext::class);

        [$_, $reviews] = $this->reviewsUtil->getReviews(
            $languageContext->getActiveId(),
            $this->getReviewsLanguages(),
            spec: $pager->getSpec()
        );

        $vars['reviews'] = $reviews;
        $vars['pager'] = $pager;

        return $vars;
    }

    protected function getReviewsLanguages(): array
    {
        return [];
    }

    protected function getItemsOnPage(): int
    {
        return 50;
    }

    private function getPager(): \Pager
    {
        $totals = $this->reviewsUtil->getReviewsTotals();

        $pager = new \Pager(page: $this->page);
        $pager->setOnPage($this->getItemsOnPage());
        $pager->setTotal($totals['count']);

        return $pager;
    }
}
