import { createFileRoute } from "@tanstack/react-router";
import { exportProviders, exportProvidersWithKeys } from "../../proxy/providers";

export const Route = createFileRoute("/api/providers/export")({
  server: {
    handlers: {
      GET: ({ request }: { request: Request }) => {
        const url = new URL(request.url);
        const includeKeys = url.searchParams.get("includeKeys") === "true";

        const json = includeKeys ? exportProvidersWithKeys() : exportProviders();

        const filename = includeKeys
          ? `llm-inspector-providers-${Date.now()}.json`
          : `llm-inspector-providers-safe-${Date.now()}.json`;

        return new Response(json, {
          headers: {
            "Content-Type": "application/json",
            "Content-Disposition": `attachment; filename="${filename}"`,
          },
        });
      },
    },
  },
});
