<?php
/**
 * SEO Manager Status Check
 * Verifies all components are working correctly
 */

session_start();
$issues = [];
$warnings = [];
$success = [];

require_once 'db_config.php';

// Check 1: Database connection
try {
    $db = $conn;  // Use the connection from db_config.php
    if ($db->connect_error) {
        $issues[] = "Database connection failed: " . $db->connect_error;
    } else {
        $success[] = "✅ Database connection OK";
    }
} catch (Exception $e) {
    $issues[] = "Database connection error: " . $e->getMessage();
}

// Check 2: Required PHP files exist
$required_files = [
    'seo_plugin.php',
    'seo_readability.php',
    'seo_keywords.php',
    'seo_schema.php',
    'seo_analytics.php',
    'admin_seo_manager.php'
];

foreach ($required_files as $file) {
    if (file_exists($file)) {
        $success[] = "✅ $file exists";
    } else {
        $issues[] = "❌ Missing file: $file";
    }
}

// Check 3: Admin authentication
if (isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true) {
    $success[] = "✅ Admin session active";
} else {
    $warnings[] = "⚠️ Not logged in as admin (required to use SEO Manager)";
}

// Check 4: Database tables
if (isset($db) && !$db->connect_error) {
    $tables = $db->query("SHOW TABLES LIKE 'seo_%'");
    if ($tables && $tables->num_rows >= 2) {
        $success[] = "✅ SEO database tables exist (" . $tables->num_rows . " tables)";
    } else {
        $warnings[] = "⚠️ SEO tables not created yet - Visit seo_db_init.php to initialize";
    }
}

// Check 5: PHP version
if (version_compare(PHP_VERSION, '7.4.0') >= 0) {
    $success[] = "✅ PHP version: " . PHP_VERSION;
} else {
    $warnings[] = "⚠️ PHP version " . PHP_VERSION . " (recommended 7.4+)";
}

?>
<!DOCTYPE html>
<html>
<head>
    <title>SEO Manager Status Check</title>
    <style>
        body { 
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
            padding: 40px; 
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .container { 
            max-width: 700px; 
            margin: 0 auto; 
            background: white; 
            padding: 40px; 
            border-radius: 10px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
        }
        h1 { 
            color: #667eea;
            margin-bottom: 10px;
            font-size: 28px;
        }
        .subtitle {
            color: #666;
            margin-bottom: 30px;
            font-size: 14px;
        }
        .status-section {
            margin: 25px 0;
            padding: 15px;
            border-radius: 5px;
        }
        .success-section {
            background: #f0f9ff;
            border-left: 4px solid #10b981;
        }
        .warning-section {
            background: #fffbeb;
            border-left: 4px solid #f59e0b;
        }
        .error-section {
            background: #fef2f2;
            border-left: 4px solid #ef4444;
        }
        .status-item {
            padding: 8px 0;
            font-size: 14px;
            line-height: 1.6;
        }
        .section-title {
            font-weight: 600;
            margin-bottom: 10px;
            color: #333;
        }
        .links {
            background: #f3f4f6;
            padding: 20px;
            border-radius: 5px;
            margin-top: 30px;
        }
        .links h3 {
            margin: 0 0 15px 0;
            color: #333;
            font-size: 14px;
        }
        .link-item {
            margin: 10px 0;
        }
        .link-item a {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
        }
        .link-item a:hover {
            text-decoration: underline;
        }
        .code {
            background: #f3f4f6;
            padding: 10px;
            border-radius: 4px;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            color: #333;
        }
        .alert {
            padding: 15px;
            margin: 20px 0;
            border-radius: 5px;
        }
        .alert-success {
            background: #ecfdf5;
            border: 1px solid #10b981;
            color: #065f46;
        }
        .alert-warning {
            background: #fffbeb;
            border: 1px solid #f59e0b;
            color: #92400e;
        }
        .alert-error {
            background: #fef2f2;
            border: 1px solid #ef4444;
            color: #7f1d1d;
        }
        button {
            background: #667eea;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-weight: 500;
            margin-top: 10px;
        }
        button:hover {
            background: #764ba2;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔍 SEO Manager Status Check</h1>
        <p class="subtitle">System health verification</p>

        <?php if (!empty($issues)): ?>
        <div class="alert alert-error">
            <strong>⚠️ Issues Found:</strong> Please fix these before using SEO Manager
        </div>
        <div class="status-section error-section">
            <div class="section-title">❌ Critical Issues</div>
            <?php foreach ($issues as $issue): ?>
                <div class="status-item"><?php echo htmlspecialchars($issue); ?></div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <?php if (!empty($warnings)): ?>
        <div class="status-section warning-section">
            <div class="section-title">⚠️ Warnings</div>
            <?php foreach ($warnings as $warning): ?>
                <div class="status-item"><?php echo htmlspecialchars($warning); ?></div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <?php if (!empty($success)): ?>
        <div class="status-section success-section">
            <div class="section-title">✅ Everything OK</div>
            <?php foreach ($success as $msg): ?>
                <div class="status-item"><?php echo htmlspecialchars($msg); ?></div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <div class="links">
            <h3>🚀 Next Steps</h3>
            
            <div class="link-item">
                <strong>1. Initialize Database (Required)</strong><br>
                If you see "SEO tables not created" warning above:
                <div class="code" style="margin-top: 8px;">
                    <a href="seo_db_init.php" style="color: #667eea; text-decoration: none;">
                        → Visit: seo_db_init.php
                    </a>
                </div>
            </div>

            <div class="link-item">
                <strong>2. Access SEO Manager</strong><br>
                After initialization and admin login:
                <div class="code" style="margin-top: 8px;">
                    <a href="admin_seo_manager.php" style="color: #667eea; text-decoration: none;">
                        → Visit: admin_seo_manager.php
                    </a>
                </div>
            </div>

            <div class="link-item">
                <strong>3. Setup Documentation</strong><br>
                Read the complete setup guide:
                <div class="code" style="margin-top: 8px;">
                    <a href="SEO_SETUP_GUIDE.md" style="color: #667eea; text-decoration: none;">
                        → Read: SEO_SETUP_GUIDE.md
                    </a>
                </div>
            </div>
        </div>

        <div style="text-align: center; margin-top: 30px; color: #999; font-size: 12px;">
            <p>Refresh this page anytime to check status</p>
        </div>
    </div>
</body>
</html>
