import { json } from "@sveltejs/kit";
import type { RequestHandler } from "./$types";

/**
 * Lightweight endpoint polled by the client every 60 seconds.
 *
 * The server-side `handle` hook in hooks.server.ts already runs before this
 * handler is reached. That hook:
 *   1. Validates the access token JWT expiry
 *   2. Refreshes the token pair when the access token is within 60 s of expiry
 *   3. Redirects to /login (303) if both tokens are unusable
 *
 * So by the time we get here we know the session is still alive — we just
 * return a 200 with a minimal body. If the hook redirected, the client fetch
 * will see a non-200 (or a redirect to /login) and can react accordingly.
 */
export const GET: RequestHandler = async ({ locals }) => {
  const accessToken = locals.session?.accessToken ?? null;

  if (!accessToken) {
    return json({ authenticated: false }, { status: 401 });
  }

  return json({ authenticated: true });
};
