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

if (!Auth::user()) {
    json_out(['ok' => false, 'error' => 'unauthorized'], 401);
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    json_out(['ok' => false, 'error' => 'POST required'], 405);
}

$raw = file_get_contents('php://input');
$data = is_string($raw) && $raw !== '' ? json_decode($raw, true) : $_POST;
$id = (int) ($data['id'] ?? 0);
if ($id <= 0) {
    json_out(['ok' => false, 'error' => 'invalid id'], 400);
}

try {
    ReportRepository::markViewed($id, (int) Auth::user()['id']);
    json_out(['ok' => true, 'id' => $id]);
} catch (Throwable $e) {
    error_log('report_viewed: ' . $e->getMessage());
    json_out(['ok' => false, 'error' => 'failed'], 500);
}
