import { github, USED_CALLBACK_URL } from "@/server/auth/oauth";
import { generateState } from "arctic";
import { cookies } from "next/headers";

export const dynamic = "force-dynamic";

export async function GET(): Promise<Response> {
	const state = generateState();
	
	// Verwende die zentral definierte Callback-URL
	// Die redirectURI ist bereits im GitHub-Client konfiguriert
	const url = await github.createAuthorizationURL(state, {
		scopes: ["user"],
	});
	const ONE_HOUR = 60 * 60;

	cookies().set("github_oauth_state", state, {
		path: "/",
		secure: false, //process.env.NODE_ENV === "production", -- can't be used until SSL certificate is provided by IT
		httpOnly: true,
		maxAge: ONE_HOUR,
		sameSite: "lax",
	});

	// Log zur Fehlersuche
	console.log(`GitHub OAuth redirect zu: ${url.toString()}`);
	console.log(`Verwendete Callback-URL: ${USED_CALLBACK_URL}`);

	return Response.redirect(url);
}
