import { NextResponse } from "next/server"

export async function POST(request: Request) {
	try {
		const body = await request.json()
		const {
			apiKey,
			page = 1,
			limit = 500,
			sort = "createdAt",
			order = "desc",
		} = body

		if (!apiKey) {
			return NextResponse.json(
				{ error: "API key is required" },
				{ status: 400 },
			)
		}

		const response = await fetch(
			"https://api.supermemory.ai/v3/documents/documents",
			{
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					Authorization: `Bearer ${apiKey}`,
				},
				body: JSON.stringify({
					page,
					limit,
					sort,
					order,
				}),
			},
		)

		if (!response.ok) {
			const errorData = await response.json().catch(() => ({}))
			return NextResponse.json(
				{ error: errorData.message || `API error: ${response.status}` },
				{ status: response.status },
			)
		}

		const data = await response.json()
		return NextResponse.json(data)
	} catch (error) {
		console.error("Graph API error:", error)
		return NextResponse.json(
			{ error: "Failed to fetch documents" },
			{ status: 500 },
		)
	}
}
