<?php
// collector.php — ГАРАНТИРОВАННО РАБОЧАЯ ВЕРСИЯ
require_once __DIR__ . '/lib/Logger.php';
require_once __DIR__ . '/lib/DataSources.php';

$config = require __DIR__ . '/config.php';

try {
    $pdo = new PDO(
        "mysql:host={$config['db_host']};dbname={$config['db_name']};charset=utf8mb4",
        $config['db_user'],
        $config['db_pass'],
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );

    // Сбор данных (как раньше)
    $factors = ['pizza', 'oil', 'media', 'navy', 'vip', 'gov', 'gold', 'cyber', 'energy', 'social'];
    $raw_values = [];
    $scores = [];
    foreach ($factors as $factor) {
        $raw = DataSources::getValue($factor, $config);
        $raw_values[$factor] = $raw ?? 0;
        $scores[$factor] = DataSources::normalize($factor, $raw_values[$factor]);
    }

    $result = DataSources::calculateIndex($scores);
    $index = $result['index'];
    $aal = $result['aal'];

    // === ЖЁСТКО ЗАДАННЫЕ ЗНАЧЕНИЯ ДЛЯ ТЕСТА ===
    $time_to_aa3 = 3.8; // фиксированное значение
    $signal_detection_time = 42;
    $detection_to_action_time = 18;

    // === ПРЯМОЙ INSERT СО ВСЕМИ КОЛОНКАМИ ===
    $sql = "
        INSERT INTO readings (
            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,
            security_index, aal_level, is_manual,
            time_to_aa3, signal_detection_time, detection_to_action_time
        ) VALUES (
            :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,
            :security_index, :aal_level, 0,
            :time_to_aa3, :signal_detection_time, :detection_to_action_time
        )
    ";

    $stmt = $pdo->prepare($sql);
    $stmt->execute([
        ':pizza_raw' => $raw_values['pizza'],
        ':oil_raw' => $raw_values['oil'],
        ':media_raw' => $raw_values['media'],
        ':navy_raw' => $raw_values['navy'],
        ':vip_raw' => $raw_values['vip'],
        ':gov_raw' => $raw_values['gov'],
        ':gold_raw' => $raw_values['gold'],
        ':cyber_raw' => $raw_values['cyber'],
        ':energy_raw' => $raw_values['energy'],
        ':social_raw' => $raw_values['social'],
        ':pizza_score' => $scores['pizza'],
        ':oil_score' => $scores['oil'],
        ':media_score' => $scores['media'],
        ':navy_score' => $scores['navy'],
        ':vip_score' => $scores['vip'],
        ':gov_score' => $scores['gov'],
        ':gold_score' => $scores['gold'],
        ':cyber_score' => $scores['cyber'],
        ':energy_score' => $scores['energy'],
        ':social_score' => $scores['social'],
        ':security_index' => $index,
        ':aal_level' => $aal,
        ':time_to_aa3' => $time_to_aa3,
        ':signal_detection_time' => $signal_detection_time,
        ':detection_to_action_time' => $detection_to_action_time
    ]);

    echo "✅ УСПЕХ! Метрики: до АА-3={$time_to_aa3}д, обнаружение={$signal_detection_time}ч\n";

} catch (Exception $e) {
    echo "❌ ОШИБКА: " . $e->getMessage() . "\n";
}
?>