<?php

function smarty_function_get_product_reviews($params, &$smarty)
{
    if (!isset($params['id_product'])) {
        throw new InvalidArgumentException('Missing parameter \'id_product\'');
    }

    $review = \KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService(\KupShop\CatalogBundle\Util\ReviewsUtil::class);
    $reviews = $review->getProductReviews(
        (int) $params['id_product'],
        null,
        $params['languages'] ?? [],
        $params['limit'] ?? null,
        $params['offset'] ?? null
    );

    $result = [
        'reviews' => $reviews,
        'reviews_count' => $review->getProductReviewsCount((int) $params['id_product'], null, $params['languages'] ?? []),
        'user_already_rated' => $review->hasUserAlreadyRated($params['id_product']),
        'overview' => $review->getProductReviewsOverview((int) $params['id_product'], null, $params['languages'] ?? []),
    ];

    if (!empty($params['assign'])) {
        $smarty->assign($params['assign'], $result);
    } else {
        return $result;
    }
}
