<?php
// Include PHP version check
require_once __DIR__ . '/includes/version_check.php';

// Include session manager and initialize session
require_once __DIR__ . '/includes/session_manager.php';
SessionManager::initialize();

// Include header (which includes all dependencies)
require_once 'includes/header.php';

// Check permissions after header.php loads all functions
if (!isLoggedIn() || !isAdmin()) {
    $_SESSION['alert'] = displayAlert($lang_alerts['ALERT_ERROR_NO_PERMISSION_DEBUG'] ?? 'You do not have permission to access this page.', 'danger');
    redirectTo('index.php');
    exit;
}

// Get server timezone and time
$serverTimezone = new DateTimeZone(TIMEZONE);
$serverTime = new DateTime('now', $serverTimezone);

// Get PHP version and loaded extensions
$phpVersion = phpversion();
$loadedExtensions = get_loaded_extensions();
$phpVersionStatus = getPhpVersionStatus();
$phpFeatures = checkPhpFeatures();
$securityRecommendations = getSecurityRecommendations();

// Get current memory usage
$memoryUsage = memory_get_usage(true);
$memoryLimit = ini_get('memory_limit');

// Get server software info
$serverSoftware = $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown';

// Display debug information
?>
<div class="container py-4">
    <h1><?php echo $lang_admin['DEBUG_INFO_HEADING'] ?? 'System Debug Information'; ?></h1>
    
    <?php displayPhpVersionWarning(); ?>
    
    <div class="card mb-3">
        <div class="card-header">
            <h5 class="card-title mb-0"><?php echo $lang_admin['DEBUG_CARD_TIME_INFO_TITLE'] ?? 'Time Information'; ?></h5>
        </div>
        <div class="card-body">
            <p><strong><?php echo $lang_admin['DEBUG_LABEL_SERVER_TIMEZONE'] ?? 'Server Timezone:'; ?></strong> <?php echo TIMEZONE; ?></p>
            <p><strong><?php echo $lang_admin['DEBUG_LABEL_CURRENT_SERVER_TIME'] ?? 'Current Server Time:'; ?></strong> <?php echo $serverTime->format(DATETIME_FORMAT); ?></p>
            <p><strong><?php echo $lang_admin['DEBUG_LABEL_DEFAULT_DATE_FORMAT'] ?? 'Default Date Format:'; ?></strong> <?php echo DATE_FORMAT; ?></p>
            <p><strong><?php echo $lang_admin['DEBUG_LABEL_DEFAULT_DATETIME_FORMAT'] ?? 'Default DateTime Format:'; ?></strong> <?php echo DATETIME_FORMAT; ?></p>
        </div>
    </div>

    <div class="card mb-3">
        <div class="card-header">
            <h5 class="card-title mb-0"><?php echo $lang_admin['DEBUG_CARD_PHP_INFO_TITLE'] ?? 'PHP Information'; ?></h5>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <p><strong><?php echo $lang_admin['DEBUG_LABEL_PHP_VERSION'] ?? 'PHP Version:'; ?></strong>
                        <span class="badge bg-<?php echo $phpVersionStatus['status'] === 'optimal' ? 'success' : ($phpVersionStatus['status'] === 'good' ? 'primary' : ($phpVersionStatus['status'] === 'deprecated' ? 'warning' : 'danger')); ?>">
                            <?php echo htmlspecialchars($phpVersion); ?>
                        </span>
                    </p>
                    <p><strong><?php echo $lang_admin['DEBUG_LABEL_STATUS'] ?? 'Status:'; ?></strong>
                        <?php
                        $status_key = 'PHP_STATUS_' . strtoupper($phpVersionStatus['status']);
                        echo $lang_admin[$status_key] ?? ucfirst($phpVersionStatus['status']);
                        ?>
                    </p>
                    <p><strong><?php echo $lang_admin['DEBUG_LABEL_MINIMUM_REQUIRED'] ?? 'Minimum Required:'; ?></strong> <?php echo htmlspecialchars($phpVersionStatus['minimum']); ?></p>
                    <p><strong><?php echo $lang_admin['DEBUG_LABEL_RECOMMENDED'] ?? 'Recommended:'; ?></strong> <?php echo htmlspecialchars($phpVersionStatus['recommended']); ?></p>
                </div>
                <div class="col-md-6">
                    <p><strong><?php echo $lang_admin['DEBUG_LABEL_MEMORY_USAGE'] ?? 'Memory Usage:'; ?></strong> <?php echo number_format($memoryUsage / 1024 / 1024, 2); ?> MB</p>
                    <p><strong><?php echo $lang_admin['DEBUG_LABEL_MEMORY_LIMIT'] ?? 'Memory Limit:'; ?></strong> <?php echo $memoryLimit; ?></p>
                    <p><strong><?php echo $lang_admin['DEBUG_LABEL_SERVER_SOFTWARE'] ?? 'Server Software:'; ?></strong> <?php echo htmlspecialchars($serverSoftware); ?></p>
                    <p><strong><?php echo $lang_admin['DEBUG_LABEL_SAPI'] ?? 'SAPI:'; ?></strong> <?php echo htmlspecialchars(php_sapi_name()); ?></p>
                </div>
            </div>
        </div>
    </div>

    <div class="card mb-3">
        <div class="card-header">
            <h5 class="card-title mb-0"><?php echo $lang_admin['DEBUG_CARD_PHP_EXTENSIONS_STATUS_TITLE'] ?? 'PHP Extensions Status'; ?></h5>
        </div>
        <div class="card-body">
            <div class="row">
                <?php foreach ($phpFeatures as $extension => $loaded): ?>
                    <div class="col-md-4 col-sm-6 mb-2">
                        <span class="badge bg-<?php echo $loaded ? 'success' : 'danger'; ?> me-2">
                            <?php echo $loaded ? '✓' : '✗'; ?>
                        </span>
                        <?php echo htmlspecialchars($extension); ?>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>

    <?php if (!empty($securityRecommendations)): ?>
    <div class="card mb-3">
        <div class="card-header">
            <h5 class="card-title mb-0"><?php echo $lang_admin['DEBUG_CARD_SECURITY_RECOMMENDATIONS_TITLE'] ?? 'Security Recommendations'; ?></h5>
        </div>
        <div class="card-body">
            <ul class="list-unstyled">
                <?php foreach ($securityRecommendations as $recommendation): ?>
                    <li><i class="bi bi-shield-alt text-warning me-2"></i><?php echo htmlspecialchars($recommendation); ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>
    <?php endif; ?>

    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0"><?php echo $lang_admin['DEBUG_CARD_LOADED_EXTENSIONS_TITLE'] ?? 'Loaded Extensions'; ?></h5>
        </div>
        <div class="card-body">
            <ul class="list-unstyled">
                <?php foreach ($loadedExtensions as $extension): ?>
                    <li><?php echo htmlspecialchars($extension); ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
