import { NextResponse } from "next/server";

export async function POST(request) {
    try {
        const body = await request.json();
        const { websitename, emailname } = body;

        if (!websitename) {
            return NextResponse.json({ error: "Website name is required" }, { status: 400 });
        }

        const flaskUrl = "https://report.rankrunners.net/get-report";

        const response = await fetch(flaskUrl, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({ websitename, emailname }),
        });

        if (!response.ok) {
            const errorData = await response.json();
            throw new Error(
                `Flask backend responded with status ${response.status} and message: ${
                    errorData.error || "Unknown error"
                }`
            );
        }

        const data = await response.json();

        return NextResponse.json(data);
    } catch (error) {
        console.error("Detailed error in getreport API:", error);
        return NextResponse.json({ error: "Internal Server Error", details: error.message }, { status: 500 });
    }
}
