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

$conn = get_db_connection();

// Create website_settings table
$sql = "CREATE TABLE IF NOT EXISTS `website_settings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `setting_key` varchar(100) NOT NULL,
  `setting_value` text DEFAULT NULL,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `setting_key` (`setting_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";

if ($conn->query($sql) === TRUE) {
    echo "Table website_settings created successfully\n";
} else {
    echo "Error creating table: " . $conn->error . "\n";
}

// Insert default settings if they don't exist
$defaults = [
    'logo' => '/wp-content/uploads/2022/05/CrestChimney-Logo-W-1024x160.png',
    'favicon' => '/wp-content/uploads/2022/05/CrestChimney-Favicon-150x150.png',
    'website_url' => 'http://localhost',
    'website_name' => 'Crest Chimney',
    'website_title' => 'Crest Chimney - Fireplace & Chimney Services',
    'phone_number' => ''
];

foreach ($defaults as $key => $value) {
    $stmt = $conn->prepare("INSERT IGNORE INTO website_settings (setting_key, setting_value) VALUES (?, ?)");
    $stmt->bind_param("ss", $key, $value);
    if ($stmt->execute()) {
        echo "Inserted default setting: $key\n";
    } else {
        echo "Error inserting $key: " . $stmt->error . "\n";
    }
    $stmt->close();
}

echo "Database setup completed!\n";
$conn->close();
