import { NextRequest, NextResponse } from "next/server";
import { cookies } from "next/headers";

export async function GET(_req: NextRequest) {
  const cookieStore = await cookies();
  const authCookieName =
    process.env.AUTH_COOKIE_NAME || "mintel_gatekeeper_session";
  const session = cookieStore.get(authCookieName);

  if (!session?.value) {
    return NextResponse.json({ authenticated: false }, { status: 401 });
  }

  let identity = "Guest";
  let company = null;
  try {
    const payload = JSON.parse(session.value);
    identity = payload.identity || "Guest";
    company = payload.company || null;
  } catch (_e) {
    // Old format probably just the password
  }

  return NextResponse.json({
    authenticated: true,
    identity: identity,
    company: company,
  });
}
