import { NextResponse } from "next/server";

import { setAuthCookies } from "@/lib/auth/cookies";

export const dynamic = "force-dynamic";

/** Receives OAuth tokens from the client callback and sets httpOnly session cookies. */
export async function POST(req: Request) {
  const body = await req.json().catch(() => null);
  const accessToken = body?.access_token;
  const refreshToken = body?.refresh_token;
  const expiresIn = Number(body?.expires_in ?? 900);
  if (!accessToken || !refreshToken) {
    return NextResponse.json({ error: "Missing tokens" }, { status: 400 });
  }
  const out = NextResponse.json({ ok: true });
  return setAuthCookies(out, accessToken, refreshToken, expiresIn);
}
