<?php

declare(strict_types=1);

namespace Gls\GlsPoland\PrestaShop\Hook\Admin\Legacy;

use Gls\GlsPoland\Controller\Admin\ConsignmentController;
use Gls\GlsPoland\PrestaShop\Hook\PrestaShopVersionAwareHookInterface;
use Gls\GlsPoland\PrestaShop\Hook\VersionRange;
use Gls\GlsPoland\Translation\TranslatorAwareTrait;
use PrestaShopBundle\Security\Voter\PageVoter;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Twig\Environment;

final class DisplayAdminOrdersListBefore implements PrestaShopVersionAwareHookInterface
{
    use TranslatorAwareTrait;

    public const HOOK_NAME = 'displayAdminOrdersListBefore';

    private $authorizationChecker;
    private $requestStack;
    private $urlGenerator;
    private $twig;

    public function __construct(AuthorizationCheckerInterface $authorizationChecker, RequestStack $requestStack, UrlGeneratorInterface $urlGenerator, Environment $twig)
    {
        $this->authorizationChecker = $authorizationChecker;
        $this->requestStack = $requestStack;
        $this->urlGenerator = $urlGenerator;
        $this->twig = $twig;
    }

    public static function getVersionRange(): VersionRange
    {
        return new VersionRange(null, '1.7.7');
    }

    public function execute(array $parameters): string
    {
        if (!$this->authorizationChecker->isGranted(PageVoter::CREATE, ConsignmentController::TAB_NAME)) {
            return '';
        }

        $session = null;
        $request = $this->requestStack->getCurrentRequest();
        if (null !== $request && $request->hasSession()) {
            $session = $request->getSession();
        }

        return $this->twig->render('@Modules/glspoland/views/templates/hook/legacy/admin_orders_list_before.html.twig', [
            'session' => $session,
            'bulk_actions' => [
                [
                    'name' => $this->getTranslator()->trans('Utwórz nadania GLS', [], 'Modules.Glspoland.Order'),
                    'url' => $this->urlGenerator->generate('admin_gls_poland_orders_legacy_bulk_create_consignments'),
                    'icon' => 'truck',
                ],
            ],
        ]);
    }
}

