import type { RequestHandler } from '@sveltejs/kit';
import { getValidAccessToken, readOAuthCredentials, getBeatLeaderSessionCookieHeader } from '$lib/server/beatleaderAuth';
import { getSteamIdFromCookies } from '$lib/server/steam';

export const GET: RequestHandler = async ({ cookies }) => {
  try {
    const token = await getValidAccessToken(cookies);
    const creds = readOAuthCredentials();
    const steamId = getSteamIdFromCookies(cookies);
    const payload = {
      connected: Boolean(token),
      hasCreds: Boolean(creds && creds.client_id && creds.client_secret),
      hasSession: Boolean(getBeatLeaderSessionCookieHeader(cookies)),
      hasSteam: Boolean(steamId),
      steamId: steamId ?? null
    };
    return new Response(JSON.stringify(payload), { headers: { 'content-type': 'application/json' } });
  } catch (err) {
    const payload = { connected: false, hasCreds: false, hasSession: false, hasSteam: false, steamId: null };
    return new Response(JSON.stringify(payload), { headers: { 'content-type': 'application/json' } });
  }
};


