import { readStreams } from "@/lib/db"

export async function GET(req: Request) {
  const { searchParams } = new URL(req.url)
  const host = searchParams.get("host") ?? "localhost"
  const port = searchParams.get("port") ?? "8888"

  const streams = readStreams()

  const lines = ["#EXTM3U"]
  streams.forEach((s, i) => {
    const chno = i + 1
    lines.push(`#EXTINF:-1 tvg-id="${s.id}" tvg-name="${s.name}" tvg-chno="${chno}" group-title="DecapStream",${chno}. ${s.name} [${s.id}] ${s.resolution} ${s.fps}fps`)
    lines.push(`http://${host}:${port}/live/${s.id}`)
  })

  return new Response(lines.join("\n"), {
    headers: {
      "Content-Type": "application/x-mpegurl",
      "Content-Disposition": 'attachment; filename="decap-stream.m3u"',
    },
  })
}
