<?php
declare(strict_types=1);
require_once __DIR__ . '/../../src/bootstrap.php';

Auth::check();

$days = (int) ($_GET['days'] ?? 14);
if ($days <= 0) {
    $days = 14;
}

try {
    $payload = GraphRepository::dashboardData($days);
    json_out(['ok' => true, 'data' => $payload]);
} catch (Throwable $e) {
    error_log('graphs api: ' . $e->getMessage());
    $out = ['ok' => false, 'error' => 'graphs_load_failed'];
    if (cfg('debug')) {
        $out['hint'] = $e->getMessage();
    }
    json_out($out, 500);
}
