<?php

namespace KupShop\CatalogBundle\Controller;

use KupShop\CatalogBundle\Enum\Reviews\ReviewsOrdering;
use KupShop\CatalogBundle\Util\ReviewsUtil;
use KupShop\CatalogBundle\View\ReviewsListView;
use KupShop\CatalogBundle\View\ReviewsView;
use KupShop\KupShopBundle\Routing\SimpleTranslatedRoute;
use KupShop\KupShopBundle\Views\View;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ReviewsController extends AbstractController
{
    /**
     * @Route("/reviews/")
     */
    public function reviewsAction(Request $request, ReviewsView $view)
    {
        $view->setRequest($request);

        return $view->getResponse();
    }

    /**
     * @Route("/reviews/{id}/", requirements={"id":"\d+"})
     */
    public function productReviewAction(Request $request, ReviewsView $view, int $id)
    {
        $view->setRequest($request);
        $view->setProductId($id);

        return $view->getResponse();
    }

    /**
     * @Route("/reviews/{id}/verify/", requirements={"id":"\d+"})
     */
    public function productReviewVerifyAction(Request $request, ReviewsView $view, int $id)
    {
        $view->setRequest($request);
        $view->setProductId($id);
        $view->setTemplate('block.reviews.verify.form.tpl');

        return $view->getResponse();
    }

    #[SimpleTranslatedRoute('reviews-list')]
    public function reviewsList(Request $request, ReviewsListView $view): Response
    {
        return $view
            ->setPage((int) $request->get('page', 1))
            ->getResponse();
    }

    #[Route('/_shop/reviews/{productId}/', requirements: ['id' => '\d+'])]
    public function ajaxProductReviews(Request $request, ReviewsUtil $reviewsUtil, int $productId): Response
    {
        $view = new class extends View {
            public ?int $productId = null;
            public array $languages = [];
            public ?int $offset = null;
            public ?string $ordering = null;

            protected $template = 'reviews/product.reviews.body.tpl';

            protected ReviewsUtil $reviewsUtil;

            public function setReviewsUtil(ReviewsUtil $reviewsUtil): void
            {
                $this->reviewsUtil = $reviewsUtil;
            }

            public function getTemplateVariables()
            {
                $ordering = $this->ordering ? ReviewsOrdering::fromString($this->ordering) : null;

                return [
                    'body_product_id' => $this->productId,
                    'ordering' => $this->ordering,
                    'reviews' => $this->reviewsUtil->getProductReviews(
                        productId: $this->productId,
                        languages: $this->languages,
                        offset: $this->offset,
                        ordering: $ordering,
                    ),
                ];
            }
        };
        $view->setReviewsUtil($reviewsUtil);

        $view->productId = $productId;
        $view->languages = $request->get('languages', []);
        $view->offset = $request->get('offset') ? (int) $request->get('offset') : null;
        $view->ordering = $request->get('ordering');

        return $view->getResponse();
    }
}
