<?php
/**
 * version_check.php
 * Prüft ob kritische Dateien aktuell sind
 */

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

$pageTitle = 'Version-Check';

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

// Dateien zum Prüfen
$filesToCheck = [
    [
        'name' => 'index.php',
        'path' => dirname(__DIR__) . '/index.php',
        'features' => [
            'Hidden-Filter' => 'show_hidden',
            'Bulk-Actions' => 'bulkActionsBar',
            'Column-Manager' => 'columnManager'
        ]
    ],
    [
        'name' => 'backup.php',
        'path' => dirname(__DIR__) . '/backup.php',
        'features' => [
            'Backup-Verzeichnis' => 'BACKUP_DIR',
            'Email-Konfiguration' => 'admin_email.txt'
        ]
    ],
    [
        'name' => 'bulk_hide.php',
        'path' => dirname(__DIR__) . '/bulk_hide.php',
        'features' => [
            'Hide-Funktion' => 'hide',
            'Hidden-Spalte' => 'hidden'
        ]
    ],
    [
        'name' => 'helpers.php',
        'path' => dirname(__DIR__) . '/helpers.php',
        'features' => [
            'makeLinksClickable' => 'makeLinksClickable',
            'renderTableCell' => 'renderTableCell'
        ]
    ],
    [
        'name' => 'login.php',
        'path' => dirname(__DIR__) . '/login.php',
        'features' => [
            'Auto-Width' => 'width: auto',
            'Password-Toggle' => 'togglePassword'
        ]
    ]
];

$results = [];

foreach ($filesToCheck as $fileInfo) {
    $result = [
        'name' => $fileInfo['name'],
        'exists' => file_exists($fileInfo['path']),
        'size' => 0,
        'size_formatted' => '',
        'modified' => '',
        'age_days' => 0,
        'features' => []
    ];
    
    if ($result['exists']) {
        $result['size'] = filesize($fileInfo['path']);
        $result['size_formatted'] = formatBytes($result['size']);  // Nutzt die aus config.php!
        $mtime = filemtime($fileInfo['path']);
        $result['modified'] = date('d.m.Y H:i:s', $mtime);
        $result['age_days'] = floor((time() - $mtime) / 86400);
        
        // Features prüfen
        $content = file_get_contents($fileInfo['path']);
        foreach ($fileInfo['features'] as $featureName => $searchString) {
            $result['features'][$featureName] = (strpos($content, $searchString) !== false);
        }
    }
    
    $results[] = $result;
}

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

<main class="backend-main">

<style>
.version-grid {
    display: grid;
    gap: 20px;
}

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

.version-card h3 {
    margin: 0 0 15px 0;
    font-size: 18px;
    color: #333;
    display: flex;
    align-items: center;
    gap: 10px;
}

.file-meta {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 15px;
    margin-bottom: 20px;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 6px;
}

.meta-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.meta-label {
    font-size: 12px;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.meta-value {
    font-size: 14px;
    font-weight: 600;
    color: #333;
}

.features-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 10px;
}

.feature-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: #f8f9fa;
    border-radius: 4px;
    font-size: 14px;
}

.feature-status {
    font-size: 16px;
}

.feature-name {
    font-family: 'Monaco', 'Courier New', monospace;
    color: #333;
}

.file-not-found {
    padding: 20px;
    background: #fff3cd;
    border: 1px solid #ffc107;
    border-radius: 6px;
    color: #856404;
}

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

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

<h1>🔄 Version-Check</h1>
<p style="color: #666; margin-bottom: 30px;">
    Prüft ob kritische Dateien vorhanden sind und erwartete Features enthalten.
</p>

<div class="version-grid">
    <?php foreach ($results as $result): ?>
    <div class="version-card">
        <h3>
            <?php if ($result['exists']): ?>
                ✅
            <?php else: ?>
                ❌
            <?php endif; ?>
            <?php echo htmlspecialchars($result['name']); ?>
        </h3>
        
        <?php if ($result['exists']): ?>
            <!-- File Meta -->
            <div class="file-meta">
                <div class="meta-item">
                    <span class="meta-label">Größe</span>
                    <span class="meta-value"><?php echo $result['size_formatted']; ?></span>
                </div>
                <div class="meta-item">
                    <span class="meta-label">Geändert am</span>
                    <span class="meta-value"><?php echo $result['modified']; ?></span>
                </div>
                <div class="meta-item">
                    <span class="meta-label">Alter</span>
                    <span class="meta-value"><?php echo $result['age_days']; ?> Tage</span>
                </div>
            </div>
            
            <!-- Features -->
            <?php if (!empty($result['features'])): ?>
            <h4 style="margin: 0 0 10px 0; font-size: 14px; color: #666;">Features:</h4>
            <div class="features-list">
                <?php foreach ($result['features'] as $featureName => $hasFeature): ?>
                <div class="feature-item">
                    <span class="feature-status">
                        <?php echo $hasFeature ? '✅' : '❌'; ?>
                    </span>
                    <span class="feature-name"><?php echo htmlspecialchars($featureName); ?></span>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        <?php else: ?>
            <div class="file-not-found">
                ⚠️ Datei nicht gefunden!
            </div>
        <?php endif; ?>
    </div>
    <?php endforeach; ?>
</div>

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

</main>

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