<?php

declare(strict_types=1);

require_once dirname(__DIR__, 2) . '/feca_mailshots_plugin/src/autoload.php';

use FecaMailshots\WordPress\ProductionWordPressFacade;

$GLOBALS['feca_registered_menu_callback'] = null;

function add_action(string $hook, callable $callback): void
{
}

function add_menu_page(...$args): void
{
    $GLOBALS['feca_registered_menu_callback'] = $args[4] ?? null;
}

function add_submenu_page(...$args): void
{
}

$facade = new ProductionWordPressFacade();
$facade->addMenuPage(
    'FECA Mailshots',
    'FECA Mailshots',
    'manage_options',
    'feca-mailshot',
    static function (): void {
        throw new RuntimeException('database connection failed');
    }
);

$callback = $GLOBALS['feca_registered_menu_callback'];
if (!is_callable($callback)) {
    fwrite(STDERR, "Expected guarded menu callback to be registered\n");
    exit(1);
}

ob_start();
$callback();
$html = (string) ob_get_clean();

if (strpos($html, 'Mailshots plugin error: database connection failed') === false) {
    fwrite(STDERR, "Expected guarded callback to render an admin error notice\n");
    fwrite(STDERR, $html . "\n");
    exit(1);
}

echo "Production facade error boundary regression test passed\n";
