import { type NextRequest, NextResponse } from "next/server"
import fs from "fs"
import path from "path"

export async function GET(request: NextRequest) {
  const searchParams = request.nextUrl.searchParams
  const filePath = searchParams.get("path")
  const sectionId = searchParams.get("sectionId")

  if (!filePath && !sectionId) {
    return NextResponse.json({ error: "Path or sectionId is required" }, { status: 400 })
  }

  if (filePath?.startsWith("custom/")) {
    // Custom sections have their content stored in localStorage on the client
    // Return empty content, client will handle it
    return NextResponse.json({ content: "", isCustom: true })
  }

  try {
    const fullPath = path.join(process.cwd(), filePath || "")
    const content = fs.readFileSync(fullPath, "utf-8")
    return NextResponse.json({ content, isCustom: false })
  } catch (error) {
    console.error("Error reading file:", error)
    return NextResponse.json({ error: "File not found" }, { status: 404 })
  }
}
