import { NextRequest, NextResponse } from "next/server";

export async function GET(req: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
    const { path: pathArray } = await params;
    const path = pathArray?.join("/") || "";
    const token = req.headers.get("authorization");
    const host = req.headers.get("host");

    try {
        const response = await fetch(`http://backend:8080/api/${path}${req.nextUrl.search}`, {
            method: "GET",
            headers: {
                "Authorization": token || "",
                "Content-Type": "application/json",
                "X-Forwarded-Host": host || "",
                "X-Original-Host": host || "",
            },
        });

        const data = await response.json();
        return NextResponse.json(data, { status: response.status });
    } catch (error) {
        console.error("API proxy error:", error);
        return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
    }
}

export async function PUT(req: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
    const { path: pathArray } = await params;
    const path = pathArray?.join("/") || "";
    const token = req.headers.get("authorization");
    const host = req.headers.get("host");
    const body = await req.json();

    try {
        const response = await fetch(`http://backend:8080/api/${path}${req.nextUrl.search}`, {
            method: "PUT",
            headers: {
                "Authorization": token || "",
                "Content-Type": "application/json",
                "X-Forwarded-Host": host || "",
                "X-Original-Host": host || "",
            },
            body: JSON.stringify(body),
        });

        const data = await response.json();
        return NextResponse.json(data, { status: response.status });
    } catch (error) {
        console.error("API proxy error:", error);
        return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
    }
}

export async function POST(req: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
    const { path: pathArray } = await params;
    const path = pathArray?.join("/") || "";
    const token = req.headers.get("authorization");
    const host = req.headers.get("host");
    const body = await req.json();

    try {
        const response = await fetch(`http://backend:8080/api/${path}${req.nextUrl.search}`, {
            method: "POST",
            headers: {
                "Authorization": token || "",
                "Content-Type": "application/json",
                "X-Forwarded-Host": host || "",
                "X-Original-Host": host || "",
            },
            body: JSON.stringify(body),
        });

        const data = await response.json();
        return NextResponse.json(data, { status: response.status });
    } catch (error) {
        console.error("API proxy error:", error);
        return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
    }
}