<?php
 
$driverIcons = [
    'local' => 'fa-hard-drive',
    'ftp' => 'fa-server',
    's3' => 'fa-cloud',
    'webdav' => 'fa-globe',
];
$statusColors = [
    'ok' => 'text-green-500',
    'warning' => 'text-amber-500',
    'error' => 'text-red-500',
];
?>
<?php if (!empty($data)): ?>
<div class="flex items-center justify-between mb-2">
    <span class="text-xs text-gray-500"><?= count($data) ?> server<?= count($data) !== 1 ? 's' : '' ?></span>
    <a href="/admin/settings?tab=storage" class="text-[11px] text-primary-600 hover:text-primary-700 font-medium">Manage</a>
</div>
<div class="space-y-2">
    <?php foreach ($data as $storage): ?>
    <?php
        $du = MediaStorage::liveDiskUsage($storage);
        $diskTotal = $du['disk_total'];
        $diskUsed = $du['disk_used'];
        $maxPct = (int) ($storage['max_usage_pct'] ?? 90);
        $usedPct = $diskTotal > 0 ? round($diskUsed / $diskTotal * 100) : 0;
        $health = $storage['health_status'] ?? 'ok';
        $barColor = $usedPct >= $maxPct ? 'red' : ($usedPct >= $maxPct * 0.8 ? '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 <?= $driverIcons[$storage['driver'] ?? 'local'] ?? 'fa-hard-drive' ?> text-gray-400 text-xs"></i>
                <span class="text-xs font-medium text-gray-800"><?= htmlspecialchars($storage['name']) ?></span>
                <?php if (($storage['status'] ?? '') === 'active' && ($storage['is_default'] ?? 0)): ?>
                    <span class="text-[9px] bg-blue-100 text-blue-600 px-1 py-0.5 rounded font-medium">Default</span>
                <?php endif; ?>
            </div>
            <i class="fa-solid fa-circle text-[6px] <?= $statusColors[$health] ?? 'text-gray-300' ?>"></i>
        </div>
        <?php if ($diskTotal > 0): ?>
        <div class="w-full bg-gray-100 rounded-full h-1.5 mb-1">
            <div class="bg-<?= $barColor ?>-500 h-1.5 rounded-full" style="width: <?= min(100, $usedPct) ?>%"></div>
        </div>
        <div class="flex items-center justify-between text-[10px] text-gray-400">
            <span><?= Format::fileSize($diskUsed) ?> / <?= Format::fileSize($diskTotal) ?></span>
            <span class="font-medium text-<?= $barColor ?>-600"><?= $usedPct ?>%</span>
        </div>
        <?php else: ?>
        <div class="text-[10px] text-gray-400"><?= number_format((int)($storage['file_count'] ?? 0)) ?> files</div>
        <?php endif; ?>
        <?php if (!empty($storage['health_message']) && $health !== 'ok'): ?>
        <div class="text-[10px] text-<?= $health === 'error' ? 'red' : 'amber' ?>-600 mt-1">
            <i class="fa-solid fa-triangle-exclamation mr-0.5"></i> <?= htmlspecialchars($storage['health_message']) ?>
        </div>
        <?php endif; ?>
    </div>
    <?php endforeach; ?>
</div>
<?php else: ?>
<div class="text-center py-8">
    <i class="fa-solid fa-hard-drive text-gray-200 text-2xl mb-2"></i>
    <p class="text-xs text-gray-400">No storage servers configured</p>
    <a href="/admin/settings?tab=storage" class="text-xs text-primary-600 hover:text-primary-700 mt-1 inline-block">Add storage</a>
</div>
<?php endif; ?>
