<?php
// /api/components.php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');

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

    $stmt = $pdo->query("
        SELECT 
            timestamp,
            pizza_score, oil_score, media_score, navy_score, vip_score,
            gov_score, gold_score, cyber_score, energy_score, social_score,
            security_index
        FROM readings 
        ORDER BY timestamp DESC 
        LIMIT 24
    ");
    
    $components = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode([
        'success' => true,
        'period' => '24 hours',
        'components' => $components
    ]);

} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['error' => 'Service error']);
}
?>