const BASE_URL =
    "https://irisinfotech.sgp1.cdn.digitaloceanspaces.com/apps/doctor-prescription/";

export async function GET(request: Request) {
    try {
        const { searchParams } = new URL(request.url);

        const path =
            searchParams.get("path") ??
            "templates/html/public/regular.html";

        const response = await fetch(BASE_URL + path);

        if (!response.ok) {
            return new Response(
                `Failed to fetch template (${response.status})`,
                { status: response.status }
            );
        }

        const html = await response.text();

        return new Response(html, {
            headers: {
                "Content-Type": "text/html",
            },
        });
    } catch (error) {
        console.error("Template fetch error:", error);

        return new Response("Unable to fetch template", {
            status: 500,
        });
    }
}