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

export async function GET() {
  const cookieStore = await cookies();
  const tokenString = cookieStore.get("jumpa_token")?.value;

  if (!tokenString) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  try {
    const payloadBase64 = tokenString.split(".")[1];
    const payloadBuffer = Buffer.from(payloadBase64, "base64");
    const user = JSON.parse(payloadBuffer.toString("utf-8"));
    return NextResponse.json(user);
  } catch {
    return NextResponse.json({ error: "Invalid Token" }, { status: 401 });
  }
}
