<?php
 
$servers = $data['servers'] ?? [];
$queue = $data['queue'] ?? [];
$statusIcons = [
    'online' => ['fa-circle-check', 'text-green-500'],
    'offline' => ['fa-circle-xmark', 'text-gray-400'],
    'error' => ['fa-circle-exclamation', 'text-red-500'],
    'unknown' => ['fa-circle-question', 'text-gray-300'],
];
$queueTotal = array_sum($queue);
$par = $data['paid_at_risk'] ?? ['failed' => 0, 'refunded' => 0, 'refund_pending' => 0];
?>

<?php if (!empty($data['all_offline'])): ?>
<div class="mb-3 p-2 rounded-lg bg-red-50 border border-red-200 text-[11px] text-red-700">
    <i class="fa-solid fa-triangle-exclamation mr-1"></i><strong>All transcode servers offline</strong> while paid jobs are queued — nothing is being processed.
</div>
<?php endif; ?>
<?php if (($par['failed'] ?? 0) > 0): ?>
<div class="mb-3 p-2 rounded-lg <?= ($par['refund_pending'] ?? 0) > 0 ? 'bg-red-50 border border-red-200 text-red-700' : 'bg-amber-50 border border-amber-200 text-amber-700' ?> text-[11px]">
    <i class="fa-solid fa-coins mr-1"></i>
    <strong><?= (int)$par['failed'] ?> paid video<?= (int)$par['failed'] !== 1 ? 's' : '' ?> failing</strong>
    — auto-retrying until delivered (no manual action, no refund).
    <a href="/admin/settings?tab=transcode&sub=queue" class="underline font-medium">Queue</a>
</div>
<?php endif; ?>


<div class="grid grid-cols-4 gap-2 mb-3">
    <div class="text-center p-2 rounded-lg bg-gray-50">
        <div class="text-sm font-bold text-gray-500"><?= $queue['pending'] ?? 0 ?></div>
        <div class="text-[9px] text-gray-400 uppercase">Pending</div>
    </div>
    <div class="text-center p-2 rounded-lg bg-blue-50">
        <div class="text-sm font-bold text-blue-600"><?= $queue['processing'] ?? 0 ?></div>
        <div class="text-[9px] text-blue-400 uppercase">Active</div>
    </div>
    <div class="text-center p-2 rounded-lg bg-green-50">
        <div class="text-sm font-bold text-green-600"><?= $queue['completed'] ?? 0 ?></div>
        <div class="text-[9px] text-green-400 uppercase">Done</div>
    </div>
    <div class="text-center p-2 rounded-lg <?= ($queue['failed'] ?? 0) > 0 ? 'bg-red-50' : 'bg-gray-50' ?>">
        <div class="text-sm font-bold <?= ($queue['failed'] ?? 0) > 0 ? 'text-red-600' : 'text-gray-500' ?>"><?= $queue['failed'] ?? 0 ?></div>
        <div class="text-[9px] <?= ($queue['failed'] ?? 0) > 0 ? 'text-red-400' : 'text-gray-400' ?> uppercase">Failed</div>
    </div>
</div>


<?php if (!empty($servers)): ?>
<div class="flex items-center justify-between mb-2">
    <span class="text-xs text-gray-500"><?= count($servers) ?> server<?= count($servers) !== 1 ? 's' : '' ?></span>
    <a href="/admin/settings?tab=transcode&sub=servers" class="text-[11px] text-primary-600 hover:text-primary-700 font-medium">Manage</a>
</div>
<div class="space-y-2">
    <?php foreach ($servers as $srv): ?>
    <?php
        $st = $srv['status'] ?? 'unknown';
        $icon = $statusIcons[$st] ?? $statusIcons['unknown'];
        $paused = !($srv['is_active'] ?? 1);
        $load = (int)($srv['active_jobs'] ?? 0);
        $max = (int)($srv['max_jobs'] ?? 1);
        $loadPct = $max > 0 ? round($load / $max * 100) : 0;
        $loadColor = $loadPct >= 100 ? 'red' : ($loadPct >= 75 ? 'amber' : 'blue');
    ?>
    <div class="p-2.5 rounded-lg border border-gray-100">
        <div class="flex items-center justify-between mb-1.5">
            <div class="flex items-center gap-2">
                <i class="fa-solid <?= $icon[0] ?> <?= $icon[1] ?> text-xs"></i>
                <span class="text-xs font-medium text-gray-800"><?= htmlspecialchars($srv['name']) ?></span>
                <?php if ($paused): ?>
                    <span class="text-[9px] bg-gray-100 text-gray-500 px-1 py-0.5 rounded font-medium">Paused</span>
                <?php endif; ?>
            </div>
            <span class="text-[10px] text-gray-400"><?= $load ?>/<?= $max ?> jobs</span>
        </div>
        <div class="w-full bg-gray-100 rounded-full h-1.5 mb-1">
            <div class="bg-<?= $loadColor ?>-500 h-1.5 rounded-full transition-all" style="width: <?= min(100, $loadPct) ?>%"></div>
        </div>
        <?php if (!empty($srv['last_error']) && $st === 'error'): ?>
        <div class="text-[10px] text-red-600 mt-1 truncate">
            <i class="fa-solid fa-triangle-exclamation mr-0.5"></i> <?= htmlspecialchars($srv['last_error']) ?>
        </div>
        <?php endif; ?>
    </div>
    <?php endforeach; ?>
</div>
<?php else: ?>
<div class="text-center py-4">
    <i class="fa-solid fa-microchip text-gray-200 text-2xl mb-2"></i>
    <p class="text-xs text-gray-400">No transcode servers configured</p>
    <a href="/admin/settings?tab=transcode&sub=servers" class="text-xs text-primary-600 hover:text-primary-700 mt-1 inline-block">Add server</a>
</div>
<?php endif; ?>
