<?php
// quick-fix.php - БЫСТРОЕ СОЗДАНИЕ БАЗЫ ДАННЫХ
header('Content-Type: text/plain; charset=utf-8');
echo "=== БЫСТРОЕ ИСПРАВЛЕНИЕ БАЗЫ ДАННЫХ ===\n";

$config = [
    'db_host' => 'localhost',
    'db_name' => 'giftfqdj_avatiah_security', 
    'db_user' => 'giftfqdj_admin',
    'db_pass' => 'ashdod1960'
];

try {
    $pdo = new PDO(
        "mysql:host={$config['db_host']};dbname={$config['db_name']};charset=utf8mb4",
        $config['db_user'],
        $config['db_pass']
    );
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "✅ Подключение к базе успешно\n";
    
    // Создаем таблицу
    $sql = "CREATE TABLE IF NOT EXISTS readings (
        id INT AUTO_INCREMENT PRIMARY KEY,
        timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
        security_index DECIMAL(3,1),
        aal_level INT,
        is_manual BOOLEAN DEFAULT FALSE,
        pizza_raw DECIMAL(5,2), oil_raw DECIMAL(10,2), media_raw INT,
        navy_raw INT, vip_raw INT, gov_raw INT, gold_raw DECIMAL(10,2),
        cyber_raw INT, energy_raw DECIMAL(5,2), social_raw DECIMAL(5,2),
        pizza_score DECIMAL(3,1), oil_score DECIMAL(3,1), media_score DECIMAL(3,1),
        navy_score DECIMAL(3,1), vip_score DECIMAL(3,1), gov_score DECIMAL(3,1),
        gold_score DECIMAL(3,1), cyber_score DECIMAL(3,1), energy_score DECIMAL(3,1),
        social_score DECIMAL(3,1)
    )";
    
    $pdo->exec($sql);
    echo "✅ Таблица 'readings' создана\n";
    
    // Добавляем тестовые данные (как на вашем изображении)
    $stmt = $pdo->prepare("INSERT INTO readings (
        security_index, aal_level, 
        pizza_raw, oil_raw, media_raw, navy_raw, vip_raw, gov_raw, gold_raw, cyber_raw, energy_raw, social_raw,
        pizza_score, oil_score, media_score, navy_score, vip_score, gov_score, gold_score, cyber_score, energy_score, social_score
    ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    
    $data = [
        2.7, 4,  // security_index, aal_level
        2.5, 75.5, 1200, 3, 8, 2, 45.5, 2, 60.0, 2.5,  // raw values
        4.2, 3.1, 2.8, 5.0, 2.0, 3.3, 2.9, 3.3, 2.2, 3.1  // scores
    ];
    
    $stmt->execute($data);
    echo "✅ Тестовые данные добавлены (индекс: 2.7, AAL: 4)\n";
    echo "🎉 БАЗА ДАННЫХ ГОТОВА! Обновите страницу сайта.\n";
    
} catch (Exception $e) {
    echo "❌ ОШИБКА: " . $e->getMessage() . "\n";
    echo "Создаем резервный API...\n";
    
    // Создаем резервный API файл
    file_put_contents('api/backup-latest.php', '<?php
    header("Content-Type: application/json");
    echo json_encode({
        "security_index": 2.7,
        "aal_level": 4, 
        "timestamp": "' . date('Y-m-d H:i:s') . '",
        "is_manual": false,
        "components": {
            "pizza": {"raw": 2.5, "score": 4.2},
            "oil": {"raw": 75.5, "score": 3.1},
            "media": {"raw": 1200, "score": 2.8},
            "navy": {"raw": 3, "score": 5.0},
            "vip": {"raw": 8, "score": 2.0},
            "gov": {"raw": 2, "score": 3.3},
            "gold": {"raw": 45.5, "score": 2.9},
            "cyber": {"raw": 2, "score": 3.3},
            "energy": {"raw": 60.0, "score": 2.2},
            "social": {"raw": 2.5, "score": 3.1}
        }
    }); ?>');
    
    echo "✅ Создан резервный API файл\n";
}
?>