import { NextResponse } from "next/server";

export async function GET(request: Request) {
    const url = new URL(request.url);
    const to = url.searchParams.get("to");

    if (!to) {
        return NextResponse.redirect(process.env.NEXT_PUBLIC_SOCKET_URL || 'http://localhost:8080');
    }

    const decodedUrl = Buffer.from(to, "base64").toString("utf-8");
    try {
        new URL(decodedUrl);
    } catch {
        return NextResponse.redirect(process.env.NEXT_PUBLIC_SOCKET_URL || 'http://localhost:8080');
    }
    return new Response(null, {
        status: 302,
        headers: {
            Location: decodedUrl,
        },
    });
}
