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

try {
    // Add columns one by one if they don't exist
    
    // Add header_font_size column if it doesn't exist
    $stmt = $pdo->query("SHOW COLUMNS FROM manual_room_allocations LIKE 'header_font_size'");
    $column_exists = $stmt->rowCount() > 0;
    if (!$column_exists) {
        $pdo->exec("ALTER TABLE manual_room_allocations ADD COLUMN header_font_size INT DEFAULT 24");
    }
    
    // Add header_text_color column if it doesn't exist
    $stmt = $pdo->query("SHOW COLUMNS FROM manual_room_allocations LIKE 'header_text_color'");
    $column_exists = $stmt->rowCount() > 0;
    if (!$column_exists) {
        $pdo->exec("ALTER TABLE manual_room_allocations ADD COLUMN header_text_color VARCHAR(7) DEFAULT '#000000'");
    }
    
    // Add header_font_weight column if it doesn't exist
    $stmt = $pdo->query("SHOW COLUMNS FROM manual_room_allocations LIKE 'header_font_weight'");
    $column_exists = $stmt->rowCount() > 0;
    if (!$column_exists) {
        $pdo->exec("ALTER TABLE manual_room_allocations ADD COLUMN header_font_weight VARCHAR(10) DEFAULT 'bold'");
    }
    
    // Add selected_text_font_size column if it doesn't exist
    $stmt = $pdo->query("SHOW COLUMNS FROM manual_room_allocations LIKE 'selected_text_font_size'");
    $column_exists = $stmt->rowCount() > 0;
    if (!$column_exists) {
        $pdo->exec("ALTER TABLE manual_room_allocations ADD COLUMN selected_text_font_size INT DEFAULT 14");
    }
    
    // Add selected_text_color column if it doesn't exist
    $stmt = $pdo->query("SHOW COLUMNS FROM manual_room_allocations LIKE 'selected_text_color'");
    $column_exists = $stmt->rowCount() > 0;
    if (!$column_exists) {
        $pdo->exec("ALTER TABLE manual_room_allocations ADD COLUMN selected_text_color VARCHAR(7) DEFAULT '#000000'");
    }
    
    // Add selected_text_font_weight column if it doesn't exist
    $stmt = $pdo->query("SHOW COLUMNS FROM manual_room_allocations LIKE 'selected_text_font_weight'");
    $column_exists = $stmt->rowCount() > 0;
    if (!$column_exists) {
        $pdo->exec("ALTER TABLE manual_room_allocations ADD COLUMN selected_text_font_weight VARCHAR(10) DEFAULT 'normal'");
    }
    
    echo "Table 'manual_room_allocations' updated successfully with new columns!\n";
    
    // Update existing records with default values for new columns
    $updateSql = "UPDATE manual_room_allocations 
                  SET header_font_size = 24,
                      header_text_color = '#000000',
                      header_font_weight = 'bold',
                      selected_text_font_size = 14,
                      selected_text_color = '#000000',
                      selected_text_font_weight = 'normal'
                  WHERE header_font_size IS NULL 
                     OR header_text_color IS NULL 
                     OR header_font_weight IS NULL
                     OR selected_text_font_size IS NULL
                     OR selected_text_color IS NULL
                     OR selected_text_font_weight IS NULL";
    
    $stmt = $pdo->prepare($updateSql);
    $count = $stmt->execute();
    
    echo "Updated $count existing records with default values for new columns.\n";
    
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>