<?php
/**
 * 2026 Paysera
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * @author Paysera <plugins@paysera.com>
 * @copyright 2026 Paysera
 * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
 * International Registered Trademark & Property of Paysera
 */

declare(strict_types=1);

namespace Paysera\PrestashopPayments\Provider;

use Paysera\CheckoutSdk\Entity\PaymentOrder\RedirectUrls;
use Paysera\PrestashopPayments\Logger\PayseraLogger;
use PrestaShop\PrestaShop\Adapter\LegacyContext;

if (!defined('_PS_VERSION_')) {
    exit;
}

class UrlProvider
{
    private \Context $context;

    public function __construct(LegacyContext $legacyContext)
    {
        $this->context = $legacyContext->getContext();
    }

    public function create(\Cart $cart): RedirectUrls
    {
        return new RedirectUrls(
            $this->getSuccessUrl($cart),
            $this->getFailureUrl($cart),
            $this->getCallbackUrl(),
        );
    }

    private function getSuccessUrl(\Cart $cart): string
    {
        return $this->context->link
            ->getModuleLink('payserapayments', 'validation', ['id_cart' => $cart->id], true);
    }

    private function getFailureUrl(\Cart $cart): string
    {
        return $this->context->link
            ->getModuleLink('payserapayments', 'failure', ['id_cart' => $cart->id], true);
    }

    public function getCallbackUrl(): string
    {
        return $this->context->link
            ->getModuleLink('payserapayments', 'callback', [], true);
    }

    public function getShopBaseUrl(): string
    {
        return $this->context->shop
            ->getBaseURL(true);
    }

    public function getAdminLogsUrl(): string
    {
        $url = $this->context->link->getAdminLink('AdminLogs');
        $filter = http_build_query([
            'log' => ['filters' => ['object_type' => PayseraLogger::MODULE_NAME]],
        ]);

        return $url . (str_contains($url, '?') ? '&' : '?') . $filter;
    }
}
