<?php

declare(strict_types=1);

if (!defined('ABSPATH')) {
    define('ABSPATH', dirname(__DIR__, 2) . '/');
}

$GLOBALS['feca_bootstrap_actions'] = [];

function get_option($key, $default = null)
{
    if ($key !== \FecaMailshots\Admin\SetupAdminPage::OPTION_KEY) {
        return $default;
    }
    return [
        'db_host' => 'db.example.test',
        'db_port' => '3306',
        'db_user' => 'mailshots',
        'db_password' => 'secret',
        'mailshots_db_name' => 'mailshots',
        'members_db_name' => 'members',
    ];
}

function add_action(string $hook, callable $callback): void
{
    if (!isset($GLOBALS['feca_bootstrap_actions'][$hook])) {
        $GLOBALS['feca_bootstrap_actions'][$hook] = [];
    }
    $GLOBALS['feca_bootstrap_actions'][$hook][] = $callback;
}

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

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

require dirname(__DIR__, 2) . '/feca_mailshots_plugin/feca_mailshots_plugin.php';

$actions = $GLOBALS['feca_bootstrap_actions'];
if (!isset($actions['admin_post_feca_mailshots_setup_save'], $actions['admin_post_feca_mailshots_setup_test'])) {
    fwrite(STDERR, "Expected setup handlers to be registered when FEN DB config is missing\n");
    exit(1);
}

foreach (array_keys($actions) as $hook) {
    if (substr($hook, 0, strlen('admin_post_feca_mailshots_data_sources')) === 'admin_post_feca_mailshots_data_sources') {
        fwrite(STDERR, "DB-backed data source handlers should not register while setup is incomplete\n");
        exit(1);
    }
}

echo "Bootstrap missing FEN setup regression test passed\n";
