<?php
// Database configuration
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'mysql'; // Default AMPPS MySQL password
$db_name = 'wedding_events';

// Create connection
$conn = new mysqli($db_host, $db_user, $db_pass);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Create database
$sql = "CREATE DATABASE IF NOT EXISTS `$db_name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully or already exists.<br>";
} else {
    die("Error creating database: " . $conn->error);
}

// Select database
$conn->select_db($db_name);

// Create users table
$sql = "CREATE TABLE IF NOT EXISTS `users` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(255) NOT NULL UNIQUE,
    `email` VARCHAR(255) NOT NULL UNIQUE,
    `password` VARCHAR(255) NOT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    INDEX `idx_username` (`username`),
    INDEX `idx_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";

if ($conn->query($sql) === TRUE) {
    echo "Table 'users' created successfully or already exists.<br>";
} else {
    die("Error creating table: " . $conn->error);
}

// Create events table (without foreign key first, then add it)
$sql = "CREATE TABLE IF NOT EXISTS `events` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `user_id` INT(11) NOT NULL DEFAULT 0,
    `event_slug` VARCHAR(255) NOT NULL,
    `event_name` VARCHAR(255) NOT NULL,
    `event_type` VARCHAR(50) NOT NULL DEFAULT 'Other',
    `celebrant1` VARCHAR(255) NOT NULL,
    `celebrant2` VARCHAR(255) NULL,
    `event_date` DATE NULL,
    `programme_filename` VARCHAR(255) NOT NULL DEFAULT 'programme.pdf',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    INDEX `idx_event_slug` (`event_slug`),
    INDEX `idx_user_id` (`user_id`),
    INDEX `idx_event_type` (`event_type`),
    UNIQUE KEY `unique_user_slug` (`user_id`, `event_slug`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";

if ($conn->query($sql) === TRUE) {
    echo "Table 'events' created successfully or already exists.<br>";
} else {
    die("Error creating table: " . $conn->error);
}

// Create admins table
$sql = "CREATE TABLE IF NOT EXISTS `admins` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(255) NOT NULL UNIQUE,
    `password` VARCHAR(255) NOT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    INDEX `idx_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";

if ($conn->query($sql) === TRUE) {
    echo "Table 'admins' created successfully or already exists.<br>";
} else {
    die("Error creating table: " . $conn->error);
}

// Create default admin user if it doesn't exist
$default_username = 'admin';
$default_password = password_hash('admin123', PASSWORD_DEFAULT);

$stmt = $conn->prepare("SELECT id FROM admins WHERE username = ?");
$stmt->bind_param("s", $default_username);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    $stmt->close();
    $stmt = $conn->prepare("INSERT INTO admins (username, password) VALUES (?, ?)");
    $stmt->bind_param("ss", $default_username, $default_password);
    if ($stmt->execute()) {
        echo "Default admin user created successfully!<br>";
        echo "Username: <strong>admin</strong><br>";
        echo "Password: <strong>admin123</strong><br>";
        echo "<em>Please change this password after first login!</em><br>";
    } else {
        echo "Warning: Could not create default admin user.<br>";
    }
    $stmt->close();
} else {
    echo "Default admin user already exists.<br>";
    $stmt->close();
}

$conn->close();
echo "<br><strong>Database setup completed successfully!</strong><br>";
echo "<a href='admin.php'>Go to Admin Panel</a>";
?>

