<?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]
    );

    // Сбор данных
    $data = DataSources::collectAllData($config);
    
    // Временные метрики (фиксированные для демо)
    $time_to_aa3 = 3.8;
    $signal_detection_time = 42;
    $detection_to_action_time = 18;

    // Вставка данных
    $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' => $data['raw_data']['pizza'],
        ':oil_raw' => $data['raw_data']['oil'],
        ':media_raw' => $data['raw_data']['media'],
        ':navy_raw' => $data['raw_data']['navy'],
        ':vip_raw' => $data['raw_data']['vip'],
        ':gov_raw' => $data['raw_data']['gov'],
        ':gold_raw' => $data['raw_data']['gold'],
        ':cyber_raw' => $data['raw_data']['cyber'],
        ':energy_raw' => $data['raw_data']['energy'],
        ':social_raw' => $data['raw_data']['social'],
        ':pizza_score' => $data['scores']['pizza'],
        ':oil_score' => $data['scores']['oil'],
        ':media_score' => $data['scores']['media'],
        ':navy_score' => $data['scores']['navy'],
        ':vip_score' => $data['scores']['vip'],
        ':gov_score' => $data['scores']['gov'],
        ':gold_score' => $data['scores']['gold'],
        ':cyber_score' => $data['scores']['cyber'],
        ':energy_score' => $data['scores']['energy'],
        ':social_score' => $data['scores']['social'],
        ':security_index' => $data['index'],
        ':aal_level' => $data['aal'],
        ':time_to_aa3' => $time_to_aa3,
        ':signal_detection_time' => $signal_detection_time,
        ':detection_to_action_time' => $detection_to_action_time
    ]);

    echo "✅ УСПЕХ! Данные сохранены. Индекс: {$data['index']}, AAL: {$data['aal']}\n";
    Logger::info('Collector executed successfully', ['index' => $data['index']]);

} catch (Exception $e) {
    echo "❌ ОШИБКА: " . $e->getMessage() . "\n";
    Logger::error('Collector failed', ['error' => $e->getMessage()]);
}
?>