<?php
require_once __DIR__ . '/db_config.php';

echo "<h1>🔧 Creating Missing Tables</h1>";
echo "<p>Database: <strong>$DB_NAME</strong></p>";
echo "<hr>";

$tables_created = [];
$errors = [];

// Check if products table exists
$check = $conn->query("SHOW TABLES LIKE 'products'");
if (!$check || $check->num_rows == 0) {
    echo "<h2>Creating 'products' table...</h2>";
    $sql = "CREATE TABLE IF NOT EXISTS `products` (
        `id` INT PRIMARY KEY AUTO_INCREMENT,
        `name` VARCHAR(255) NOT NULL,
        `description` LONGTEXT,
        `price` DECIMAL(10, 2),
        `quantity` INT DEFAULT 0,
        `category_id` INT,
        `image` VARCHAR(255),
        `gallery_images` LONGTEXT,
        `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        KEY `category_id` (`category_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
    
    if ($conn->query($sql)) {
        echo "✅ Products table created successfully<br>";
        $tables_created[] = 'products';
    } else {
        echo "❌ Error creating products table: " . $conn->error . "<br>";
        $errors[] = 'products: ' . $conn->error;
    }
} else {
    echo "✅ Products table already exists<br>";
}

// Check if categories table exists
$check = $conn->query("SHOW TABLES LIKE 'categories'");
if (!$check || $check->num_rows == 0) {
    echo "<h2>Creating 'categories' table...</h2>";
    $sql = "CREATE TABLE IF NOT EXISTS `categories` (
        `id` INT PRIMARY KEY AUTO_INCREMENT,
        `name` VARCHAR(255) NOT NULL UNIQUE,
        `description` LONGTEXT,
        `image` VARCHAR(255),
        `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
    
    if ($conn->query($sql)) {
        echo "✅ Categories table created successfully<br>";
        $tables_created[] = 'categories';
    } else {
        echo "❌ Error creating categories table: " . $conn->error . "<br>";
        $errors[] = 'categories: ' . $conn->error;
    }
} else {
    echo "✅ Categories table already exists<br>";
}

echo "<hr>";
echo "<h2>📊 Summary</h2>";

if (count($tables_created) > 0) {
    echo "<p style='color: green;'><strong>✅ Tables Created:</strong> " . implode(', ', $tables_created) . "</p>";
}

if (count($errors) > 0) {
    echo "<p style='color: red;'><strong>❌ Errors:</strong></p>";
    echo "<ul>";
    foreach ($errors as $error) {
        echo "<li style='color: red;'>$error</li>";
    }
    echo "</ul>";
} else {
    echo "<p style='color: green;'><strong>✅ All tables are ready!</strong></p>";
}

echo "<hr>";
echo "<h2>Next Steps:</h2>";
echo "<ol>";
echo "<li><a href='check_db_tables.php'>Verify All Tables</a></li>";
echo "<li><a href='master_image_fix.php'>Fix Image Paths</a></li>";
echo "<li><a href='index.php'>View Homepage</a></li>";
echo "</ol>";

$conn->close();
?>
