<?php
/**
 * backup_diagnose.php
 * Zeigt Backup-Status und Größen
 */

require_once '../backend/config.php';
requireBackendAccess();

$pageTitle = 'Backup-Info';

// KEINE formatBytes() mehr hier - kommt aus config.php!

// Backup-Verzeichnis prüfen
$backupDir = dirname(__DIR__) . '/backups/';
$backupExists = is_dir($backupDir);
$backupReadable = $backupExists && is_readable($backupDir);
$backupWritable = $backupExists && is_writable($backupDir);

// Backups sammeln
$backups = [];
$totalSize = 0;
$newestBackup = null;
$oldestBackup = null;

if ($backupReadable) {
    $files = glob($backupDir . '*.sql');
    
    foreach ($files as $file) {
        $filename = basename($file);
        $size = filesize($file);
        $mtime = filemtime($file);
        
        $backups[] = [
            'filename' => $filename,
            'size' => $size,
            'size_formatted' => formatBytes($size),  // Nutzt die aus config.php!
            'date' => date('d.m.Y H:i:s', $mtime),
            'timestamp' => $mtime,
            'age_days' => floor((time() - $mtime) / 86400)
        ];
        
        $totalSize += $size;
        
        if (!$newestBackup || $mtime > $newestBackup) {
            $newestBackup = $mtime;
        }
        if (!$oldestBackup || $mtime < $oldestBackup) {
            $oldestBackup = $mtime;
        }
    }
    
    // Nach Datum sortieren (neueste zuerst)
    usort($backups, function($a, $b) {
        return $b['timestamp'] - $a['timestamp'];
    });
}

$backupCount = count($backups);

// Backend Header laden
include '../backend/layout/header.php';
?>

<main class="backend-main">

<style>
.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.info-card {
    background: white;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 20px;
}

.info-card h3 {
    margin: 0 0 15px 0;
    font-size: 16px;
    color: #666;
}

.info-row {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px solid #f0f0f0;
}

.info-row:last-child {
    border-bottom: none;
}

.info-label {
    font-weight: 500;
    color: #333;
}

.info-value {
    color: #666;
}

.status-ok {
    color: #27ae60;
    font-weight: 600;
}

.status-error {
    color: #e74c3c;
    font-weight: 600;
}

.backup-table {
    width: 100%;
    background: white;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    overflow: hidden;
}

.backup-table th {
    background: #f8f9fa;
    padding: 12px 15px;
    text-align: left;
    font-weight: 600;
    color: #333;
    border-bottom: 2px solid #e0e0e0;
}

.backup-table td {
    padding: 12px 15px;
    border-bottom: 1px solid #f0f0f0;
}

.backup-table tr:last-child td {
    border-bottom: none;
}

.backup-table tr:hover {
    background: #f8f9fa;
}

.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: #999;
}

.empty-state-icon {
    font-size: 64px;
    margin-bottom: 20px;
}

.back-link {
    display: inline-block;
    margin-top: 30px;
    color: #667eea;
    text-decoration: none;
    font-weight: 500;
}

.back-link:hover {
    text-decoration: underline;
}
</style>

<h1>💾 Backup-Informationen</h1>

<!-- Info-Grid -->
<div class="info-grid">
    <!-- Verzeichnis-Status -->
    <div class="info-card">
        <h3>📁 Verzeichnis-Status</h3>
        <div class="info-row">
            <span class="info-label">Existiert:</span>
            <span class="info-value <?php echo $backupExists ? 'status-ok' : 'status-error'; ?>">
                <?php echo $backupExists ? '✓ Ja' : '✗ Nein'; ?>
            </span>
        </div>
        <div class="info-row">
            <span class="info-label">Lesbar:</span>
            <span class="info-value <?php echo $backupReadable ? 'status-ok' : 'status-error'; ?>">
                <?php echo $backupReadable ? '✓ Ja' : '✗ Nein'; ?>
            </span>
        </div>
        <div class="info-row">
            <span class="info-label">Beschreibbar:</span>
            <span class="info-value <?php echo $backupWritable ? 'status-ok' : 'status-error'; ?>">
                <?php echo $backupWritable ? '✓ Ja' : '✗ Nein'; ?>
            </span>
        </div>
        <div class="info-row">
            <span class="info-label">Pfad:</span>
            <span class="info-value" style="font-size: 12px; word-break: break-all;">
                <?php echo htmlspecialchars($backupDir); ?>
            </span>
        </div>
    </div>
    
    <!-- Statistiken -->
    <div class="info-card">
        <h3>📊 Statistiken</h3>
        <div class="info-row">
            <span class="info-label">Anzahl Backups:</span>
            <span class="info-value"><?php echo $backupCount; ?></span>
        </div>
        <div class="info-row">
            <span class="info-label">Gesamtgröße:</span>
            <span class="info-value"><?php echo formatBytes($totalSize); ?></span>
        </div>
        <?php if ($newestBackup): ?>
        <div class="info-row">
            <span class="info-label">Neuestes:</span>
            <span class="info-value"><?php echo date('d.m.Y H:i', $newestBackup); ?></span>
        </div>
        <?php endif; ?>
        <?php if ($oldestBackup): ?>
        <div class="info-row">
            <span class="info-label">Ältestes:</span>
            <span class="info-value"><?php echo date('d.m.Y H:i', $oldestBackup); ?></span>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Backup-Liste -->
<?php if ($backupCount > 0): ?>
<h2>📋 Alle Backups</h2>
<table class="backup-table">
    <thead>
        <tr>
            <th>Dateiname</th>
            <th>Größe</th>
            <th>Erstellt am</th>
            <th>Alter (Tage)</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($backups as $backup): ?>
        <tr>
            <td><?php echo htmlspecialchars($backup['filename']); ?></td>
            <td><?php echo $backup['size_formatted']; ?></td>
            <td><?php echo $backup['date']; ?></td>
            <td><?php echo $backup['age_days']; ?> Tage</td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php else: ?>
<div class="empty-state">
    <div class="empty-state-icon">📦</div>
    <h3>Keine Backups vorhanden</h3>
    <p>Es wurden noch keine Backups erstellt.</p>
    <a href="../backup.php" style="color: #667eea; text-decoration: none; font-weight: 600;">
        → Jetzt Backup erstellen
    </a>
</div>
<?php endif; ?>

<a href="diagnose_tools.php" class="back-link">← Zurück zu Diagnose-Tools</a>

</main>

<?php include '../backend/layout/footer.php'; ?>
