import { NextResponse } from "next/server";
import { startLoginFlow } from "@/lib/threads-browser";

export const maxDuration = 300;

export async function POST(request: Request) {
  try {
    const body = (await request.json().catch(() => ({}))) as { clearSession?: boolean };
    const result = await startLoginFlow({
      clearSession: body.clearSession !== false,
    });
    return NextResponse.json(result, { status: result.success ? 200 : 400 });
  } catch (error) {
    const message = error instanceof Error ? error.message : "登入流程失敗";
    return NextResponse.json({ success: false, message }, { status: 500 });
  }
}