<?php
/**
 * Check if External API Pricing Tables Exist
 * Run this file to verify the tables were created
 */

require_once 'config.php';

$tablesToCheck = [
    'external_api_pricing' => 'External API Pricing (from other website)',
    'external_api_admin_pricing' => 'Admin Pricing for External API Orders',
    'external_api_orders' => 'External API Orders'
];

echo "<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>External API Tables Check</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background: #1e293b;
            color: #fff;
        }
        .table-check {
            background: #334155;
            border: 1px solid #475569;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
        }
        .status {
            display: inline-block;
            padding: 5px 15px;
            border-radius: 5px;
            font-weight: bold;
            margin-left: 10px;
        }
        .exists {
            background: #10b981;
            color: white;
        }
        .missing {
            background: #ef4444;
            color: white;
        }
        h1 {
            color: #60a5fa;
        }
        .info {
            background: #475569;
            padding: 15px;
            border-radius: 5px;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <h1>🔍 External API Tables Verification</h1>
    <p>Checking if required tables exist in the database...</p>
";

$allExist = true;
$results = [];

foreach ($tablesToCheck as $tableName => $description) {
    $checkQuery = "SHOW TABLES LIKE '$tableName'";
    $result = mysqli_query($conn, $checkQuery);
    $exists = $result && mysqli_num_rows($result) > 0;
    
    if (!$exists) {
        $allExist = false;
    }
    
    $results[] = [
        'name' => $tableName,
        'description' => $description,
        'exists' => $exists
    ];
    
    if ($result) {
        mysqli_free_result($result);
    }
}

foreach ($results as $result) {
    $statusClass = $result['exists'] ? 'exists' : 'missing';
    $statusText = $result['exists'] ? '✓ EXISTS' : '✗ MISSING';
    
    echo "<div class='table-check'>";
    echo "<h2>{$result['name']}</h2>";
    echo "<p>{$result['description']}</p>";
    echo "<span class='status $statusClass'>$statusText</span>";
    
    if ($result['exists']) {
        // Get table structure
        $descQuery = "DESCRIBE `{$result['name']}`";
        $descResult = mysqli_query($conn, $descQuery);
        
        if ($descResult) {
            echo "<div class='info'>";
            echo "<strong>Columns:</strong><br>";
            echo "<table style='width:100%; margin-top:10px; border-collapse:collapse;'>";
            echo "<tr style='background:#1e293b;'><th style='text-align:left; padding:5px;'>Column</th><th style='text-align:left; padding:5px;'>Type</th><th style='text-align:left; padding:5px;'>Null</th><th style='text-align:left; padding:5px;'>Key</th></tr>";
            
            while ($row = mysqli_fetch_assoc($descResult)) {
                echo "<tr style='border-bottom:1px solid #475569;'>";
                echo "<td style='padding:5px;'>{$row['Field']}</td>";
                echo "<td style='padding:5px;'>{$row['Type']}</td>";
                echo "<td style='padding:5px;'>{$row['Null']}</td>";
                echo "<td style='padding:5px;'>{$row['Key']}</td>";
                echo "</tr>";
            }
            
            echo "</table>";
            echo "</div>";
            mysqli_free_result($descResult);
        }
    } else {
        echo "<div class='info' style='background:#7f1d1d;'>";
        echo "<strong>⚠️ Table Missing!</strong><br>";
        echo "Please run the SQL file: <code>create_external_api_pricing_tables.sql</code>";
        echo "</div>";
    }
    
    echo "</div>";
}

if ($allExist) {
    echo "<div class='table-check' style='background:#065f46; border-color:#10b981;'>";
    echo "<h2 style='color:#10b981;'>✅ All Tables Exist!</h2>";
    echo "<p>The external API pricing system is ready to use.</p>";
    echo "</div>";
} else {
    echo "<div class='table-check' style='background:#7f1d1d; border-color:#ef4444;'>";
    echo "<h2 style='color:#ef4444;'>❌ Some Tables Are Missing</h2>";
    echo "<p>Please run the SQL migration file: <code>create_external_api_pricing_tables.sql</code></p>";
    echo "<p>You can run it in phpMyAdmin or via MySQL command line.</p>";
    echo "</div>";
}

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


