<?php
declare(strict_types=1);

require_once __DIR__ . '/../includes/bootstrap.php';

echo "Creating settings table...\n\n";

try {
    $pdo = Database::pdo();
    
    echo "Step 1: Creating settings table...\n";
    $pdo->exec("CREATE TABLE IF NOT EXISTS settings (
        setting_id INT AUTO_INCREMENT PRIMARY KEY,
        setting_key VARCHAR(100) NOT NULL UNIQUE,
        setting_value TEXT NULL,
        setting_type VARCHAR(50) DEFAULT 'text',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        INDEX idx_setting_key (setting_key)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    echo "✓ Settings table created\n\n";
    
    echo "Step 2: Inserting default settings...\n";
    $defaultSettings = [
        ['app_name', 'Sustainable Pest Management', 'text'],
        ['app_email', 'admin@pestmanagement.com', 'email'],
        ['app_phone', '+1234567890', 'text'],
        ['app_address', '123 Business Street, City, Country', 'textarea'],
        ['timezone', 'UTC', 'text'],
        ['date_format', 'Y-m-d', 'text'],
        ['time_format', 'H:i', 'text'],
        ['items_per_page', '20', 'number'],
        ['enable_notifications', '1', 'boolean'],
        ['enable_email_alerts', '1', 'boolean'],
        ['maintenance_mode', '0', 'boolean'],
    ];
    
    $stmt = $pdo->prepare("INSERT IGNORE INTO settings (setting_key, setting_value, setting_type) VALUES (?, ?, ?)");
    foreach ($defaultSettings as $setting) {
        $stmt->execute($setting);
    }
    echo "✓ Default settings inserted\n\n";
    
    echo "===========================================\n";
    echo "✓ Settings table created successfully!\n";
    echo "===========================================\n\n";
    
} catch (Throwable $e) {
    echo "✗ Error: " . $e->getMessage() . "\n";
    echo "File: " . $e->getFile() . "\n";
    echo "Line: " . $e->getLine() . "\n";
    exit(1);
}
