<?php
/**
 * Temporary Files Manager
 * Admin interface to manage temporary files
 */

require_once '../../config/config.php';
require_once '../../includes/functions.php';
require_once '../../includes/cache-manager.php';
require_once '../../includes/session-manager.php';
require_once '../../includes/upload-manager.php';
require_once '../../includes/login-attempts.php';

requireAdmin();

$action = $_GET['action'] ?? 'view';
$message = '';
$error = '';

$cache = getCache();
$session = getSession();
$upload = getUploadManager();
$loginAttempts = getLoginAttempts();

if ($action == 'clear_cache') {
    $count = $cache->clear();
    $message = "Cache cleared! $count files deleted.";
    $action = 'view';
} elseif ($action == 'clean_sessions') {
    $count = $session->cleanExpired();
    $message = "Expired sessions cleaned! $count sessions removed.";
    $action = 'view';
} elseif ($action == 'clean_uploads') {
    $count = $upload->clean();
    $message = "Temporary uploads cleaned! $count files deleted.";
    $action = 'view';
} elseif ($action == 'clean_attempts') {
    $count = $loginAttempts->clean();
    $message = "Old login attempts cleaned! $count records removed.";
    $action = 'view';
} elseif ($action == 'clean_all') {
    $cacheCount = $cache->clear();
    $sessionCount = $session->cleanExpired();
    $uploadCount = $upload->clean();
    $attemptsCount = $loginAttempts->clean();
    $message = "All temporary files cleaned! Cache: $cacheCount, Sessions: $sessionCount, Uploads: $uploadCount, Attempts: $attemptsCount";
    $action = 'view';
}

$cacheStats = $cache->getStats();
$sessionStats = $session->getStats();
$uploadStats = $upload->getStats();
$attemptsStats = $loginAttempts->getStats();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Temporary Files Manager - Admin Panel</title>
    <link href="../../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../../assets/vendor/fontawesome/css/all.min.css" rel="stylesheet">
    <style>
        .stats-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            text-align: center;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .stats-number {
            font-size: 32px;
            font-weight: bold;
        }
        .action-btn {
            margin: 5px;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div class="card mt-3">
                    <div class="card-header">
                        <h4><i class="fas fa-database"></i> Temporary Files Manager</h4>
                    </div>
                    <div class="card-body">
                        <?php if ($message): ?>
                            <div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
                        <?php endif; ?>
                        
                        <?php if ($error): ?>
                            <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
                        <?php endif; ?>
                        
                        <div class="row">
                            <!-- Cache Statistics -->
                            <div class="col-md-3">
                                <div class="stats-card">
                                    <div class="stats-number"><?php echo $cacheStats['total_files']; ?></div>
                                    <div class="text-muted">Cache Files</div>
                                    <div class="small"><?php echo $cacheStats['total_size_formatted']; ?></div>
                                    <div class="mt-2">
                                        <span class="badge bg-success">Valid: <?php echo $cacheStats['valid_files']; ?></span>
                                        <span class="badge bg-warning">Expired: <?php echo $cacheStats['expired_files']; ?></span>
                                    </div>
                                    <a href="?action=clear_cache" class="btn btn-danger btn-sm mt-3" onclick="return confirm('Clear all cache?')">
                                        <i class="fas fa-trash"></i> Clear Cache
                                    </a>
                                </div>
                            </div>
                            
                            <!-- Session Statistics -->
                            <div class="col-md-3">
                                <div class="stats-card">
                                    <div class="stats-number"><?php echo $sessionStats['active_sessions']; ?></div>
                                    <div class="text-muted">Active Sessions</div>
                                    <div class="small"><?php echo $sessionStats['session_name']; ?></div>
                                    <div class="mt-2">
                                        <span class="badge bg-info">Lifetime: <?php echo $sessionStats['session_lifetime'] / 60; ?> min</span>
                                    </div>
                                    <a href="?action=clean_sessions" class="btn btn-warning btn-sm mt-3" onclick="return confirm('Clean expired sessions?')">
                                        <i class="fas fa-broom"></i> Clean Sessions
                                    </a>
                                </div>
                            </div>
                            
                            <!-- Uploads Statistics -->
                            <div class="col-md-3">
                                <div class="stats-card">
                                    <div class="stats-number"><?php echo $uploadStats['total_files']; ?></div>
                                    <div class="text-muted">Temp Uploads</div>
                                    <div class="small"><?php echo $uploadStats['total_size_formatted']; ?></div>
                                    <div class="mt-2">
                                        <span class="badge bg-secondary">Max: <?php echo $uploadStats['max_file_size'] / 1024 / 1024; ?> MB</span>
                                    </div>
                                    <a href="?action=clean_uploads" class="btn btn-warning btn-sm mt-3" onclick="return confirm('Clean temporary uploads?')">
                                        <i class="fas fa-upload"></i> Clean Uploads
                                    </a>
                                </div>
                            </div>
                            
                            <!-- Login Attempts Statistics -->
                            <div class="col-md-3">
                                <div class="stats-card">
                                    <div class="stats-number"><?php echo $attemptsStats['tracked_identifiers']; ?></div>
                                    <div class="text-muted">Tracked Attempts</div>
                                    <div class="small">Failed: <?php echo $attemptsStats['total_failed_attempts']; ?></div>
                                    <div class="mt-2">
                                        <span class="badge bg-danger">Locked: <?php echo $attemptsStats['locked_identifiers']; ?></span>
                                    </div>
                                    <a href="?action=clean_attempts" class="btn btn-warning btn-sm mt-3" onclick="return confirm('Clean old login attempts?')">
                                        <i class="fas fa-shield-alt"></i> Clean Attempts
                                    </a>
                                </div>
                            </div>
                        </div>
                        
                        <div class="row mt-4">
                            <div class="col-md-12 text-center">
                                <a href="?action=clean_all" class="btn btn-danger btn-lg" onclick="return confirm('Clean ALL temporary files? This action cannot be undone.')">
                                    <i class="fas fa-trash-alt"></i> Clean All Temporary Files
                                </a>
                                <a href="temp-manager.php" class="btn btn-secondary btn-lg">
                                    <i class="fas fa-sync-alt"></i> Refresh
                                </a>
                            </div>
                        </div>
                        
                        <div class="row mt-4">
                            <div class="col-md-12">
                                <div class="alert alert-info">
                                    <i class="fas fa-info-circle"></i>
                                    <strong>Information:</strong>
                                    <ul class="mb-0 mt-2">
                                        <li>Cache files are automatically deleted after 1 hour or when expired</li>
                                        <li>Sessions expire after <?php echo $sessionStats['session_lifetime'] / 60; ?> minutes of inactivity</li>
                                        <li>Temporary uploads are automatically deleted after 24 hours</li>
                                        <li>Login attempts are tracked for <?php echo $attemptsStats['max_attempts']; ?> attempts before <?php echo $attemptsStats['lockout_time']; ?> lockout</li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>