<?php
session_start();
require_once '../config/database.php';
require_once '../includes/functions.php';

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

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    http_response_code(403);
    echo json_encode(['error' => 'Unauthorized']);
    exit();
}

$db = new Database();
$conn = $db->getConnection();

try {
    $response = [
        'monthly' => getChartData($conn, 'monthly'),
        'units' => getChartData($conn, 'units'),
        'antibiotics' => getChartData($conn, 'antibiotics')
    ];
    
    echo json_encode($response);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['error' => 'حدث خطأ في جلب البيانات']);
}
?>
