<?php

/**
 * Copyright 2024-2025 Simply.IN Sp. z o.o.
 *
 * NOTICE OF LICENSE
 *
 * Licensed under the EUPL-1.2 or later.
 * You may not use this work except in compliance with the Licence.
 *
 * Copy of the Licence is available at:
 * https://joinup.ec.europa.eu/software/page/eupl
 * It is bundled with this package in the file LICENSE.txt
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Licence is distributed on an as is basis,
 * without warranties or conditions of any kind, either express or implied.
 * Check the Licence for the specific language governing permissions
 * and limitations under the License.
 *
 * @author   Simply.IN Sp. z o.o.
 * @copyright 2024-2025 Simply.IN Sp. z o.o.
 * @license   https://joinup.ec.europa.eu/software/page/eupl
 */

declare(strict_types=1);

namespace SimplyIn\Payment;

use Configuration;
use Context;
use Customer;
use Module;
use Order;
use SimplyIn\Front\BrandColors;
use SimplyIn\Session\SimplyInSessionInterface;
use SimplyInPayments;
use Tools;

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

final class OrderConfirmationRenderer
{
    private SimplyInSessionInterface $session;

    private OrderRetryRenderer $retryRenderer;

    public function __construct(SimplyInSessionInterface $session, OrderRetryRenderer $retryRenderer)
    {
        $this->session = $session;
        $this->retryRenderer = $retryRenderer;
    }

    /**
     * The live status widget and the pay-again surface used to be two separate cards saying
     * overlapping things about the same order - this renders both into ONE template now:
     * the status widget owns the card, and the retry surface's markup (embedded, no card of its own,
     * {@see OrderRetryRenderer::render()} with `$embedded = true`) is handed in as the content the
     * error state shows instead of a static "not paid" line. `null` (nothing to offer/paid/closed)
     * falls back to that static line inside the template - the retry surface being silent is not
     * this method failing.
     */
    public function render(Module $module, Order $order): ?string
    {
        if (!$module->active || !Configuration::get('SIMPLYIN_PAYMENTS')) {
            return null;
        }

        $idCart = (int) $order->id_cart;
        $paymentInfo = SimplyInPayments::getPaymentInfo($idCart);
        $customer = new Customer((int) $order->id_customer);

        if ($customer->is_guest && $paymentInfo === false) {
            $this->session->clear();
        }

        if ($paymentInfo === false) {
            return null;
        }

        Context::getContext()->smarty->assign([
            'payment_info' => $paymentInfo,
            'current_url' => $this->currentUrl(),
            'module_path' => _MODULE_DIR_ . $module->name . '/',
            'payment_status' => $paymentInfo->status,
            'accent_color' => BrandColors::accent(),
            'font_color' => BrandColors::font(),
            'retry_html' => $this->retryRenderer->render($module, $order, true),
        ]);

        return $module->fetch('module:simplyin/views/templates/hook/payment_return.tpl');
    }

    private function currentUrl(): string
    {
        $host = isset($_SERVER['HTTP_HOST']) ? (string) $_SERVER['HTTP_HOST'] : '';
        $uri = isset($_SERVER['REQUEST_URI']) ? (string) $_SERVER['REQUEST_URI'] : '';

        return Tools::getCurrentUrlProtocolPrefix() . $host . $uri;
    }
}
