<?php
require_once __DIR__ . '/app/bootstrap.php';

if (strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? 'GET')) !== 'POST') {
    header('Allow: POST');
    send_json_response(['ok' => false, 'message' => 'HTTP metoda nije dozvoljena.'], 405);
}

if (!authenticated_session_exists() || !current_user()) {
    send_json_response([
        'ok' => false,
        'expired' => true,
        'message' => 'Sesija je istekla.',
        'login_url' => url('login.php?session=expired'),
    ], 401);
}

if (!csrf_token_is_valid((string)($_POST['csrf_token'] ?? ''))) {
    send_json_response([
        'ok' => false,
        'expired' => false,
        'message' => 'Bezbednosni token je obnovljen. Ponovite zahtev.',
        'csrf_token' => csrf_token(),
    ], 419);
}

mark_authenticated_user_activity();
send_json_response(['ok' => true] + session_timing_payload());
