<?php
/**
 * Complete Setup Verification Script
 * Checks all configurations and file dependencies
 */

session_start();
echo "<!DOCTYPE html><html><head><style>";
echo "body { font-family: Segoe UI, sans-serif; margin: 20px; background: #f5f5f5; }";
echo ".container { max-width: 1000px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }";
echo "h1 { color: #333; border-bottom: 3px solid #007bff; padding-bottom: 10px; }";
echo "h2 { color: #007bff; margin-top: 30px; }";
echo ".status { padding: 15px; margin: 10px 0; border-radius: 6px; }";
echo ".success { background: #d4edda; color: #155724; border-left: 4px solid #28a745; }";
echo ".error { background: #f8d7da; color: #721c24; border-left: 4px solid #dc3545; }";
echo ".warning { background: #fff3cd; color: #856404; border-left: 4px solid #ffc107; }";
echo ".info { background: #d1ecf1; color: #0c5460; border-left: 4px solid #17a2b8; }";
echo ".code { background: #f4f4f4; padding: 10px; border-radius: 4px; font-family: monospace; overflow-x: auto; }";
echo "table { width: 100%; border-collapse: collapse; margin: 15px 0; }";
echo "th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }";
echo "th { background: #f0f0f0; font-weight: 600; }";
echo "tr:hover { background: #f9f9f9; }";
echo ".check { font-weight: bold; }";
echo "</style></head><body>";

echo "<div class='container'>";
echo "<h1>🔍 Complete Setup Verification</h1>";

// 1. Environment Detection
echo "<h2>1. Environment Detection</h2>";
$environment = $_SERVER['HTTP_HOST'] ?? 'localhost';
$is_cpanel = (strpos($environment, 'localhost') === false && strpos($environment, '127.0.0.1') === false);

echo "<div class='status info'>";
echo "<strong>Current Domain:</strong> " . htmlspecialchars($environment) . "<br>";
echo "<strong>Environment Detected:</strong> <span class='check'>" . ($is_cpanel ? "🌐 cPanel" : "💻 Local XAMPP") . "</span>";
echo "</div>";

// 2. Database Configuration
echo "<h2>2. Database Configuration</h2>";
require_once __DIR__ . '/db_config.php';

if (isset($conn) && !$conn->connect_error) {
    echo "<div class='status success'>";
    echo "<strong>✅ Database Connection:</strong> SUCCESS<br>";
    echo "<strong>Connected to Database:</strong> " . htmlspecialchars($DB_NAME) . "<br>";
    echo "<strong>Host:</strong> " . htmlspecialchars($DB_HOST) . "<br>";
    echo "<strong>User:</strong> " . htmlspecialchars($DB_USER) . "<br>";
    echo "</div>";
    
    // Get table list
    $result = $conn->query("SHOW TABLES");
    if ($result && $result->num_rows > 0) {
        echo "<h3>Database Tables</h3>";
        echo "<div class='status success'>";
        echo "<table>";
        echo "<tr><th>Table Name</th><th>Status</th></tr>";
        $critical_tables = ['users', 'blogs', 'products', 'categories', 'orders', 'seo_settings'];
        $found_tables = [];
        while ($row = $result->fetch_row()) {
            $table = $row[0];
            $found_tables[] = $table;
            $is_critical = in_array($table, $critical_tables);
            echo "<tr>";
            echo "<td>" . htmlspecialchars($table) . "</td>";
            echo "<td>" . ($is_critical ? "✅ Critical" : "ℹ️ Support") . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        
        // Check for missing critical tables
        $missing = array_diff($critical_tables, $found_tables);
        if (!empty($missing)) {
            echo "<div class='status warning'>";
            echo "<strong>⚠️ Missing Tables:</strong> " . implode(", ", $missing);
            echo "</div>";
        }
        
        echo "</div>";
    }
    
    $conn->close();
} else {
    echo "<div class='status error'>";
    echo "<strong>❌ Database Connection Failed</strong><br>";
    echo "<strong>Error:</strong> " . ($conn->connect_error ?? "Unknown error") . "<br>";
    echo "<strong>Host:</strong> " . htmlspecialchars($DB_HOST) . "<br>";
    echo "<strong>User:</strong> " . htmlspecialchars($DB_USER) . "<br>";
    echo "<strong>Database:</strong> " . htmlspecialchars($DB_NAME) . "<br>";
    echo "</div>";
}

// 3. Required Files
echo "<h2>3. Required PHP Files</h2>";
$required_files = [
    'db_config.php' => 'Core database configuration',
    'header.php' => 'Header component',
    'footer.php' => 'Footer component',
    'api_login.php' => 'Login API',
    'api_signup.php' => 'Signup API',
    'blogs.php' => 'Blog page',
    'index.php' => 'Home page',
    'admin_seo_manager.php' => 'SEO Manager'
];

echo "<div class='status info'>";
echo "<table>";
echo "<tr><th>File</th><th>Status</th><th>Description</th></tr>";
foreach ($required_files as $file => $desc) {
    $path = __DIR__ . '/' . $file;
    $exists = file_exists($path);
    echo "<tr>";
    echo "<td>" . htmlspecialchars($file) . "</td>";
    echo "<td>" . ($exists ? "✅ Found" : "❌ Missing") . "</td>";
    echo "<td>" . htmlspecialchars($desc) . "</td>";
    echo "</tr>";
}
echo "</table>";
echo "</div>";

// 4. Data Files
echo "<h2>4. Data Files</h2>";
$data_files = [
    'data/products.json' => 'Product data',
    'data/categories.json' => 'Category data',
    'data/footer_config.json' => 'Footer configuration',
    'data/site_config.json' => 'Site configuration'
];

echo "<div class='status info'>";
echo "<table>";
echo "<tr><th>File</th><th>Status</th><th>Description</th></tr>";
foreach ($data_files as $file => $desc) {
    $path = __DIR__ . '/' . $file;
    $exists = file_exists($path);
    $size = $exists ? formatBytes(filesize($path)) : 'N/A';
    echo "<tr>";
    echo "<td>" . htmlspecialchars($file) . "</td>";
    echo "<td>" . ($exists ? "✅ Found" : "❌ Missing") . " (" . $size . ")</td>";
    echo "<td>" . htmlspecialchars($desc) . "</td>";
    echo "</tr>";
}
echo "</table>";
echo "</div>";

// 5. Test URLs
echo "<h2>5. Quick Test Links</h2>";
echo "<div class='status info'>";
echo "<ul>";
echo "<li><a href='test_db_connection.php' target='_blank'>🔌 Test Database Connection</a> - Check both local and cPanel DB</li>";
echo "<li><a href='test_data.php' target='_blank'>📊 Test Data Loading</a> - Verify JSON and database data</li>";
echo "<li><a href='seo_status.php' target='_blank'>🔍 SEO Status Check</a> - Verify SEO system</li>";
echo "<li><a href='index.php' target='_blank'>🏠 Home Page</a> - View homepage with footer</li>";
echo "<li><a href='blogs.php' target='_blank'>📝 Blog Page</a> - View blog listing</li>";
echo "<li><a href='admin_seo_manager.php?action=seo-products' target='_blank'>⚙️ SEO Manager</a> - Admin SEO tools</li>";
echo "</ul>";
echo "</div>";

// 6. Configuration Summary
echo "<h2>6. Configuration Summary</h2>";
echo "<div class='status info'>";
echo "<h3>Local Development Setup</h3>";
echo "<div class='code'>";
echo "Host: localhost<br>";
echo "User: root<br>";
echo "Password: (empty)<br>";
echo "Database: login-table<br>";
echo "</div>";

echo "<h3>cPanel Production Setup</h3>";
echo "<div class='code'>";
echo "Host: maham.usadomainhosting.com<br>";
echo "User: mahamusa<br>";
echo "Password: tCG!c&lpxW8Q<br>";
echo "Database: mahamusa_db<br>";
echo "</div>";

echo "<h3>Automatic Switching</h3>";
echo "<p>The system automatically detects whether you're on localhost or cPanel and uses the appropriate database configuration. <strong>No manual changes needed!</strong></p>";
echo "</div>";

// 7. Recent Database Activity
echo "<h2>7. Connectivity Check</h2>";
require_once __DIR__ . '/db_config.php';

if (isset($conn) && !$conn->connect_error) {
    echo "<div class='status success'>";
    echo "<strong>✅ Ready to Use</strong><br>";
    echo "All files and database are properly configured. Your website is ready to:<br>";
    echo "<ul>";
    echo "<li>✅ Process user sign in/sign up</li>";
    echo "<li>✅ Display blog pages and articles</li>";
    echo "<li>✅ Show footer with contact info</li>";
    echo "<li>✅ Manage products and orders</li>";
    echo "<li>✅ Handle SEO management</li>";
    echo "</ul>";
    echo "</div>";
    $conn->close();
} else {
    echo "<div class='status error'>";
    echo "<strong>⚠️ Database Issue Detected</strong><br>";
    echo "Please check database credentials and cPanel settings.";
    echo "</div>";
}

echo "</div>";
echo "</body></html>";

function formatBytes($bytes, $precision = 2) {
    $units = ['B', 'KB', 'MB'];
    for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
        $bytes /= 1024;
    }
    return round($bytes, $precision) . ' ' . $units[$i];
}
?>
