<?php
// pages/components/audience_list_flow.php
// Renders the flow visualization for a single Audience (newsletter list), intended for lazy loading via htmx

use Components\Models\Audience;
use Components\CampaignFlow\FlowViz;

$id = (int) mh_get('id', 0, 'int');
$refresh = (bool) mh_get('refresh', 0, 'int');

// Always buffer inline HTML
ob_start();

try {
    $model = $id > 0 ? Audience::find($id) : null;
    if (!$model) {
        ?>
        <div class="p-4 text-sm text-secondary">List not found.</div>
        <?php
        echo ob_get_clean();
        return;
    }

    if (mh_get('view_mode') === 'performance') {
        echo \Components\Ui\PerformanceDashboard::render('', $id, [
            'context' => \Components\Ui\PerformanceDashboard::CONTEXT_AUDIENCE,
            'title' => mh_lng('MAILBEEZ_PERFORMANCE_AUDIENCE', 'Performance')
        ]);
        echo ob_get_clean();
        return;
    }

    echo $model->renderFlow(!$refresh);
} catch (\Throwable $e) {
    ?>
    <div class="p-4 text-xs text-secondary">Failed to render flow.</div>
    <?php
}

echo ob_get_clean();
