<?php
// =============================================================
//  POST /api/v1/notices/mark_read   { "notice_id": NN }
//
//  Mirrors app/notice_ajax.php's mark_read action. Idempotent (INSERT
//  IGNORE — same unique-per-reader-per-notice semantics).
// =============================================================

require_once __DIR__ . '/../_bootstrap.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    Api::error(405, 'MethodNotAllowed', 'Use POST');
}

$account    = api_authenticate();
$readerType = match ($account->getTable()) {
    'users'             => 'staff',
    'rotation_nurses'   => 'nurse',
    'clinical_students' => 'student',
};

$body     = Api::body();
$noticeId = (int)($body['notice_id'] ?? 0);
if (!$noticeId) {
    Api::success(['status' => 'ignored']); // matches the web handler's silent no-op on a missing id
}

$userId = $account->getId();
$stmt = $conn->prepare("INSERT IGNORE INTO notice_reads (notice_id, reader_id, reader_type, read_at) VALUES (?, ?, ?, NOW())");
$stmt->bind_param('iis', $noticeId, $userId, $readerType);
$stmt->execute();
$stmt->close();

Api::success(['status' => 'marked_read']);
