<?php

declare(strict_types=1);

require_once dirname(__DIR__, 3) . '/system/bootstrap.php';

$currentUser = myronRequireAuthenticatedUser();

header('Content-Type: application/json; charset=utf-8');
header('X-Content-Type-Options: nosniff');

if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
    http_response_code(405);
    echo json_encode(['ok' => false, 'error' => 'POST required'], JSON_UNESCAPED_SLASHES);
    exit;
}

try {
    myronRequireCsrf($_POST['csrf_token'] ?? null);
    myronDashboardMessagesRepository()->markAsRead(
        trim((string) ($_POST['message_id'] ?? '')),
        (string) $currentUser['id']
    );

    echo json_encode(['ok' => true], JSON_UNESCAPED_SLASHES);
} catch (Throwable $exception) {
    http_response_code(400);
    echo json_encode(['ok' => false, 'error' => $exception->getMessage()], JSON_UNESCAPED_SLASHES);
}
