import { auth } from "@/auth";
import { NextResponse } from "next/server";

const GST_SERVICE = process.env.GST_SERVICE_URL ?? "http://localhost:3003";

/** Proxy: load GST portal page + fetch CAPTCHA → { sessionId, captchaBase64 } */
export async function GET() {
  const session = await auth();
  if (!session?.user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  try {
    const res = await fetch(`${GST_SERVICE}/captcha`, { cache: "no-store" });
    const data = await res.json();
    return NextResponse.json(data, { status: res.ok ? 200 : 502 });
  } catch (e) {
    return NextResponse.json({ error: `GST service unavailable: ${String(e)}` }, { status: 502 });
  }
}
