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

echo "<h2>Database Setup / Repair</h2>";

// 1. Create admin_users table
$sql = "CREATE TABLE IF NOT EXISTS admin_users (
    id INT(11) NOT NULL AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
)";
if ($conn->query($sql) === TRUE) {
    echo "Table 'admin_users' checked/created successfully.<br>";
} else {
    echo "Error creating table 'admin_users': " . $conn->error . "<br>";
}

// 2. Insert Default Admin if not exists
$check_admin = $conn->query("SELECT * FROM admin_users WHERE username = 'admin'");
if ($check_admin->num_rows == 0) {
    $pass = password_hash('admin123', PASSWORD_DEFAULT);
    $sql = "INSERT INTO admin_users (username, password) VALUES ('admin', '$pass')";
    if ($conn->query($sql) === TRUE) {
        echo "Default admin user created.<br>";
    } else {
        echo "Error creating admin user: " . $conn->error . "<br>";
    }
} else {
    echo "Admin user already exists.<br>";
}

// 3. Create services table
$sql = "CREATE TABLE IF NOT EXISTS services (
    id INT(11) NOT NULL AUTO_INCREMENT,
    title VARCHAR(100) NOT NULL,
    description TEXT NOT NULL,
    image_url VARCHAR(255) DEFAULT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
)";
if ($conn->query($sql) === TRUE) {
    echo "Table 'services' checked/created successfully.<br>";
} else {
    echo "Error creating table 'services': " . $conn->error . "<br>";
}

// 4. Create pages table
$conn->query("DROP TABLE IF EXISTS pages");
$sql = "CREATE TABLE pages (
    id INT(11) NOT NULL AUTO_INCREMENT,
    page_name VARCHAR(50) NOT NULL,
    title VARCHAR(255),
    content TEXT NOT NULL,
    PRIMARY KEY (id)
)";
if ($conn->query($sql) === TRUE) {
    echo "Table 'pages' dropped and re-created successfully.<br>";
} else {
    echo "Error creating table 'pages': " . $conn->error . "<br>";
}

// Insert Default Pages content if empty
$check_pages = $conn->query("SELECT * FROM pages");
if ($check_pages->num_rows == 0) {
    $conn->query("INSERT INTO pages (page_name, title, content) VALUES ('home_intro', 'Welcome to BALIMEEN', '“Driven by our mission to build responsibly and serve with integrity, Balimeen Private Limited provides efficient, high-quality construction services while creating meaningful employment opportunities that contribute to the growth of communities and society as a whole.”')");
    $conn->query("INSERT INTO pages (page_name, title, content) VALUES ('about_full', 'About Balimeen', 'Welcome to Balimeen Private Limited, a trusted and emerging name in the construction industry. We specialize in delivering high-quality construction services with a strong commitment to innovation, efficiency, and customer satisfaction.')");
    echo "Default page content inserted.<br>";
}

// 5. Create contact_messages table
$sql = "CREATE TABLE IF NOT EXISTS contact_messages (
    id INT(11) NOT NULL AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL,
    message TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
)";
if ($conn->query($sql) === TRUE) {
    echo "Table 'contact_messages' checked/created successfully.<br>";
} else {
    echo "Error creating table 'contact_messages': " . $conn->error . "<br>";
}

// 6. Create blogs table
$sql = "CREATE TABLE IF NOT EXISTS 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) === TRUE) {
    echo "Table 'blogs' checked/created successfully.<br>";
    
    // Check if blogs table is empty, if so, add a demo blog
    $check_blogs = $conn->query("SELECT * FROM blogs");
    if ($check_blogs->num_rows == 0) {
        $demo_title = "Welcome to Our New Website";
        $demo_content = "We are pleased to announce the launch of our new website. Check back here for the latest news and updates.";
        $demo_img = "https://images.unsplash.com/photo-1497366216548-37526070297c?auto=format&fit=crop&w=800&q=60";
        $sql_insert = "INSERT INTO blogs (title, content, image_url) VALUES ('$demo_title', '$demo_content', '$demo_img')";
        if ($conn->query($sql_insert) === TRUE) {
            echo "Sample blog post inserted.<br>";
        }
    }
} else {
    echo "Error creating table 'blogs': " . $conn->error . "<br>";
}

echo "<h3>Setup Complete. You can now login.</h3>";
echo "<a href='admin/login.php'>Go to Admin Login</a>";
?>
