<?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;

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

use Paysera\PrestashopPayments\Service\AuthorizationService;
use Paysera\PrestashopPayments\Service\BillingCountryResolver;
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
use Twig\Environment;

class PaymentOptionsProvider
{
    public function __construct(
        private AuthorizationService $authorizationService,
        private SettingsProvider $settingsProvider,
        private PaymentCountryProviderInterface $countryProvider,
        private Environment $twig,
        private BillingCountryResolver $billingCountryResolver
    ) {
    }

    /**
     * @return PaymentOption[]
     */
    public function getPaymentOptions(\Context $context, \Module $module): array
    {
        $data = $this->getPaymentOptionData($context, $module);

        if ($data === null) {
            return [];
        }

        $countrySelectHtml = $this->twig->render('payment_country_select.html.twig', [
            'countries' => $data['countries'],
            'paymentMethodsUrl' => $data['paymentMethodsUrl'],
            'selectedCountry' => $data['selectedCountry'],
            'windowMode' => $data['windowMode'],
            'failureUrl' => $data['failureUrl'],
        ]);

        $paymentOption = new PaymentOption();
        $paymentOption
            ->setCallToActionText($data['paymentTitle'])
            ->setAction($data['paymentUrl'])
            ->setLogo($data['logoPath'])
            ->setAdditionalInformation($countrySelectHtml)
            ->setInputs([
                [
                    'type' => 'hidden',
                    'name' => 'paysera_payment_method',
                    'id' => 'paysera_selected_method',
                    'value' => '',
                ],
                [
                    'type' => 'hidden',
                    'name' => 'paysera_country_code',
                    'id' => 'paysera_selected_country',
                    'value' => '',
                ],
            ])
        ;

        return [$paymentOption];
    }

    public function getPaymentOptionData(\Context $context, \Module $module): ?array
    {
        if (!$this->shouldDisplayPaymentOptions($module)) {
            return null;
        }

        return $this->buildPaymentOptionResponse($context, $module);
    }

    private function buildPaymentOptionResponse(\Context $context, \Module $module): array
    {
        return [
            'countries' => $this->countryProvider->getAvailableCountries(),
            'selectedCountry' => $this->billingCountryResolver->resolve($context),
            'paymentMethodsUrl' => $context->link->getModuleLink($module->name, 'paymentmethods', [], true),
            'paymentUrl' => $context->link->getModuleLink($module->name, 'payment', [], true),
            'failureUrl' => $context->link->getModuleLink($module->name, 'failure', ['id_cart' => $context->cart->id], true),
            'paymentTitle' => $this->settingsProvider->getPaymentTitle(),
            'logoPath' => $module->getPathUri() . 'views/img/paysera.svg',
            'windowMode' => $this->settingsProvider->getPaymentWindowMode(),
        ];
    }

    private function shouldDisplayPaymentOptions(\Module $module): bool
    {
        if (!$module->active) {
            return false;
        }

        if (!$this->authorizationService->isAuthorized()) {
            return false;
        }

        if (!$this->settingsProvider->isPaymentsEnabled()) {
            return false;
        }

        return true;
    }
}
