<?php
$pendingApproval = count(array_filter($campaigns ?? [], fn($row) => ($row['approval_status'] ?? 'not_required') === 'pending'));
$sentTotal = array_sum(array_map(fn($row) => (int)($row['sent_recipients'] ?? 0), $campaigns ?? []));
?>
<section class="workspace-hero">
    <div>
        <h2>Campaign Reports</h2>
        <p>Work the recovery queue, review campaign queue status, open detailed reports, and monitor approvals for commercial campaign operations.</p>
    </div>
    <div class="workspace-actions">
        <a class="btn secondary small" href="index.php?route=messages/campaignDashboard">Campaign Dashboard</a>
        <a class="btn secondary small" href="index.php?route=messages/campaignStudio">Campaign Studio</a>
    </div>
</section>

<section class="cards four compact-stats">
    <div class="card stat"><h3>Recovery Queue</h3><p><?= (int)($queueStats['failed_jobs'] ?? 0) ?></p><span>failed jobs waiting for action</span></div>
    <div class="card stat alt"><h3>Campaign Queue</h3><p><?= (int)($queueStats['queued_jobs'] ?? 0) ?></p><span>queued jobs pending processing</span></div>
    <div class="card stat alt"><h3>Pending Approval</h3><p><?= (int)$pendingApproval ?></p><span>campaigns awaiting approval</span></div>
    <div class="card stat alt"><h3>Total Sent</h3><p><?= (int)$sentTotal ?></p><span>recipients sent across listed campaigns</span></div>
</section>

<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2>Recovery Queue</h2><span class="badge">Jobs</span></div>
        <table>
            <thead><tr><th>ID</th><th>Type</th><th>Error</th><th>Updated</th></tr></thead>
            <tbody>
            <?php foreach (($failedQueueJobs ?? []) as $job): ?>
                <tr>
                    <td>#<?= (int)$job['id'] ?></td>
                    <td><?= e($job['job_type']) ?></td>
                    <td><?= e(mb_strimwidth((string)$job['last_error'], 0, 90, '...')) ?></td>
                    <td><?= e($job['updated_at'] ?? $job['created_at'] ?? '') ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($failedQueueJobs)): ?><tr><td colspan="4" class="muted">No failed queue jobs.</td></tr><?php endif; ?>
            </tbody>
        </table>
        <div class="stacked-badges mt-12">
            <a class="btn secondary small" href="index.php?route=settings/diagnostics">Open Diagnostics</a>
            <a class="btn secondary small" href="index.php?route=settings/providerStatus">Provider Status</a>
        </div>
    </div>
    <div class="card">
        <div class="toolbar"><h2>Campaign Queue + Reports</h2><span class="badge">Commercial campaigns</span></div>
        <table>
            <thead><tr><th>Name</th><th>Channel</th><th>Total</th><th>Sent</th><th>Approval</th><th></th></tr></thead>
            <tbody>
            <?php foreach (($campaigns ?? []) as $campaign): ?>
                <tr>
                    <td><?= e($campaign['name']) ?><br><span class="small muted"><?= e($campaign['status']) ?></span></td>
                    <td><?= e(strtoupper($campaign['channel'])) ?></td>
                    <td><?= (int)($campaign['total_recipients'] ?? 0) ?></td>
                    <td><?= (int)($campaign['sent_recipients'] ?? 0) ?></td>
                    <td><?= e($campaign['approval_status'] ?? 'not_required') ?></td>
                    <td class="actions-cell">
                        <a class="btn secondary small" href="index.php?route=messages/report&id=<?= (int)$campaign['id'] ?>">Report</a>
                        <?php if (($campaign['approval_status'] ?? 'not_required') === 'pending'): ?>
                            <a class="btn primary small" href="index.php?route=messages/approveCampaign&id=<?= (int)$campaign['id'] ?>">Approve</a>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($campaigns)): ?><tr><td colspan="6" class="muted">No campaigns queued yet.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</section>

<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2>Comparison Snapshot</h2><span class="badge">Recent reports</span></div>
        <table>
            <thead><tr><th>Campaign</th><th>Sent</th><th>Opens</th><th>Clicks</th></tr></thead>
            <tbody>
            <?php foreach (($campaignComparison ?? []) as $row): ?>
                <tr>
                    <td><a href="index.php?route=messages/report&id=<?= (int)$row['id'] ?>"><?= e($row['name']) ?></a></td>
                    <td><?= (int)($row['sent_recipients'] ?? 0) ?></td>
                    <td><?= (int)($row['unique_open_count'] ?? 0) ?></td>
                    <td><?= (int)($row['unique_click_count'] ?? 0) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($campaignComparison)): ?><tr><td colspan="4" class="muted">No campaign comparison data yet.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
    <div class="card spaced-card">
        <div class="toolbar"><h2>Report Operations</h2><span class="badge">Workflow</span></div>
        <ul class="feature-list small">
            <li>Open a campaign report to send tests, review preflight details, and inspect recipient breakdowns.</li>
            <li>Approve pending campaigns directly from the queue and send them to processing.</li>
            <li>Use diagnostics when recovery queue volume grows or provider issues affect delivery.</li>
        </ul>
    </div>
</section>
