<?php
include 'includes/db.php';

echo "Starting Database Fixes...\n";

// 1. Fix BLOGS Table
$check_blogs = $conn->query("SHOW TABLES LIKE 'blogs'");
if ($check_blogs->num_rows == 0) {
    echo "Creating 'blogs' table...\n";
    $sql_blogs = "CREATE TABLE blogs (
        id INT(11) NOT NULL AUTO_INCREMENT,
        title VARCHAR(255) NOT NULL,
        content TEXT NOT NULL,
        image_url VARCHAR(255) DEFAULT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (id)
    )";
    if ($conn->query($sql_blogs) === TRUE) {
        echo "Table 'blogs' created successfully.\n";
    } else {
        echo "Error creating 'blogs' table: " . $conn->error . "\n";
    }
} else {
    echo "Table 'blogs' already exists.\n";
}

// 2. Fix SERVICES Table
$check_services = $conn->query("SHOW COLUMNS FROM services LIKE 'image_url'");
if ($check_services->num_rows == 0) {
    echo "Column 'image_url' missing in 'services'. Adding it...\n";
    $sql_alter = "ALTER TABLE services ADD COLUMN image_url VARCHAR(255) DEFAULT NULL AFTER description";
    if ($conn->query($sql_alter) === TRUE) {
        echo "Column 'image_url' added to 'services' successfully.\n";
    } else {
        echo "Error adding column: " . $conn->error . "\n";
    }
} else {
    echo "Column 'image_url' already exists in 'services'.\n";
}

echo "Database fixes completed.\n";
?>
