<?php

declare(strict_types=1);

namespace Plugin\tnt_mcp\src\Mcp\Backend;

use JTL\Smarty\BackendSmarty;
use Laminas\Diactoros\Response;
use Psr\Http\Message\ResponseInterface;

/**
 * A {@see BackendSmarty} that records which template WOULD have been rendered, and renders nothing.
 *
 * We invoke backend controllers for their **side effect**, never for their HTML. Core's
 * `JTLSmarty::getResponse()` always fetches the template, which for our purposes is pure cost and a
 * large new failure surface: missing template variables, plugin output filters and the profiler all
 * become ways for a successful write to report itself as broken.
 *
 * `fetch()` is deliberately left intact. `IOController::saveShippingSurcharge()` and
 * `createShippingSurchargeZIP()` call `fetch('snippets/alert_list.tpl')` mid-action and need a
 * working template engine; only the final whole-page render is suppressed.
 *
 * ⚠️ **Construct this only INSIDE an entered {@see GlobalStateSandbox}.** `BackendSmarty::init()`
 * dereferences `$_SERVER['REQUEST_URI']`, reads `$_SESSION['adminTheme']` and calls
 * `Shop::Container()->getAdminAccount()->getID()`. All three are fine inside the sandbox and all
 * three are hazards outside it, so the ordering is load-bearing — {@see BackendActionInvoker} is the
 * only intended caller and a test pins the order.
 */
final class NonRenderingBackendSmarty extends BackendSmarty
{
    public ?string $renderedTemplate = null;

    public function getResponse(string $template): ResponseInterface
    {
        $this->renderedTemplate = $template;

        return new Response();
    }
}
