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

const API_BASE =
  process.env.NEXT_PUBLIC_API_BASE_URL ??
  process.env.API_BASE_URL ??
  "http://127.0.0.1:8000";
const PROXY_TIMEOUT_MS = 20000;

const REQUEST_HEADERS_TO_STRIP = new Set([
  "connection",
  "keep-alive",
  "proxy-authenticate",
  "proxy-authorization",
  "te",
  "trailer",
  "transfer-encoding",
  "upgrade",
  "host",
  "content-length",
  "accept-encoding",
]);

const RESPONSE_HEADERS_TO_STRIP = new Set([
  ...REQUEST_HEADERS_TO_STRIP,
  "content-encoding",
]);

async function proxy(request: NextRequest, path: string[]) {
  const target = new URL(`/api/v1/${path.join("/")}`, API_BASE);
  request.nextUrl.searchParams.forEach((value, key) => {
    target.searchParams.set(key, value);
  });

  const headers = new Headers();
  request.headers.forEach((value, key) => {
    if (!REQUEST_HEADERS_TO_STRIP.has(key.toLowerCase())) {
      headers.set(key, value);
    }
  });

  const init: RequestInit = {
    method: request.method,
    headers,
    redirect: "manual",
    cache: "no-store",
  };

  if (request.method !== "GET" && request.method !== "HEAD") {
    init.body = await request.arrayBuffer();
  }

  const controller = new AbortController();
  const timer = setTimeout(() => controller.abort(), PROXY_TIMEOUT_MS);
  init.signal = controller.signal;

  try {
    const response = await fetch(target, init);
    const responseHeaders = new Headers();
    response.headers.forEach((value, key) => {
      if (!RESPONSE_HEADERS_TO_STRIP.has(key.toLowerCase())) {
        responseHeaders.set(key, value);
      }
    });
    return new NextResponse(response.body, {
      status: response.status,
      headers: responseHeaders,
    });
  } catch (error) {
    const warning =
      error instanceof Error && error.name === "AbortError"
        ? "proxy_timeout"
        : "proxy_unavailable";
    return NextResponse.json(
      {
        data: null,
        meta: { generated_at: new Date().toISOString(), query: {} },
        warnings: [warning],
      },
      { status: warning === "proxy_timeout" ? 504 : 503 },
    );
  } finally {
    clearTimeout(timer);
  }
}

export async function GET(
  request: NextRequest,
  context: { params: Promise<{ path: string[] }> },
) {
  const { path } = await context.params;
  return proxy(request, path);
}

export async function POST(
  request: NextRequest,
  context: { params: Promise<{ path: string[] }> },
) {
  const { path } = await context.params;
  return proxy(request, path);
}
