import { NextRequest, NextResponse } from "next/server"
import { prisma } from "@/lib/prisma"
import { auth0 } from "@/lib/auth0"

export async function GET(request: NextRequest): Promise<NextResponse> {
  const session = await auth0.getSession()
  if (!session?.user?.sub) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
  }

  const callId = request.nextUrl.searchParams.get("callId")

  if (!callId) {
    return NextResponse.json({ error: "Missing callId parameter" }, { status: 400 })
  }

  const call = await prisma.llm_calls.findUnique({
    where: { id: callId },
    select: {
      system_prompt: true,
      user_prompt: true,
      raw_response: true,
    },
  })

  if (!call) {
    return NextResponse.json({ error: "LLM call not found" }, { status: 404 })
  }

  return NextResponse.json(
    {
      systemPrompt: call.system_prompt ?? null,
      userPrompt: call.user_prompt ?? null,
      rawResponse: call.raw_response ?? null,
    },
    {
      headers: { "Cache-Control": "public, max-age=3600, immutable" },
    },
  )
}
