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

use Paysera\PrestashopPayments\Provider\AssetUrlProvider;
use Paysera\PrestashopPayments\Provider\ReactDataProvider;

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

abstract class AbstractPayseraAdminController extends \ModuleAdminController
{
    /**
     * @var \PayseraPayments
     */
    public $module;

    public function __construct()
    {
        parent::__construct();
        $this->bootstrap = true;
    }

    public function setMedia($isNewTheme = false)
    {
        parent::setMedia($isNewTheme);

        $this->addCSS($this->module->get(AssetUrlProvider::class)->getUrl('views/css/admin-core.css'));
    }

    protected function renderReactPage(string $pageName): string
    {
        $reactDataProvider = $this->module->get(ReactDataProvider::class);
        $payseraData = $reactDataProvider->getPayseraData($this->module, $this->context, $pageName);

        $assetUrlProvider = $this->module->get(AssetUrlProvider::class);

        $this->addCSS($assetUrlProvider->getUrl('views/css/style.css'));

        \Media::addJsDef([
            'payseraData' => $payseraData,
        ]);

        $pageUrl = $assetUrlProvider->getUrl('views/js/react/' . $pageName . '.js');

        $scripts = '<script type="module" src="' . htmlspecialchars($pageUrl, ENT_QUOTES, 'UTF-8') . '"></script>';

        return $scripts . '<div id="paysera-react-root" class="paysera-react-app" data-page="'
            . htmlspecialchars($pageName, ENT_QUOTES, 'UTF-8')
            . '"></div>';
    }
}
