<?php
/**
 * BookiFlex Notification Extension
 *
 * @package    BookiFlex
 * @subpackage Notification
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Extensions\Notification\Service;

use Twig\Environment;

defined('ABSPATH') || exit;

/**
 * Renders a notification email template as an HTTP HTML response.
 * Shared by NotificationLog (snapshot-based) and NotificationPreview (fake-data-based).
 */
class NotificationRenderer
{
    public function __construct(private readonly Environment $twig) {}

    /**
     * Renders a Twig template with the given context and outputs it as an HTML page.
     * Terminates the request after output.
     */
    public function renderHtmlResponse(string $template, array $context): never
    {
        header('Content-Type: text/html; charset=utf-8');
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        echo $this->twig->render($template, $context);
        exit;
    }

    /**
     * Renders from a snapshot array: ['template' => string, 'context' => array].
     * Terminates the request after output.
     *
     * @param array{template: string, context: array} $snapshot
     */
    public function renderSnapshotResponse(array $snapshot): never
    {
        $this->renderHtmlResponse($snapshot['template'], $snapshot['context']);
    }
}