<?php
require_once '../config.php';

header('Content-Type: application/json');

try {
    $db = new Database();
    $db->query("SELECT symbol, current_price FROM cryptocurrencies WHERE is_active = 1");
    $prices = $db->resultSet();
    
    // Simulate price fluctuations (in a real app, you'd fetch from an API)
    foreach ($prices as &$price) {
        $fluctuation = (rand(-500, 500) / 10000); // ±5% fluctuation
        $price['current_price'] = $price['current_price'] * (1 + $fluctuation);
        
        // Update database with new price
        $db->query("UPDATE cryptocurrencies SET current_price = :price WHERE symbol = :symbol");
        $db->bind(':price', $price['current_price']);
        $db->bind(':symbol', $price['symbol']);
        $db->execute();
    }
    
    echo json_encode(['success' => true, 'prices' => $prices]);
    
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Failed to fetch prices']);
}
?>