import { NextResponse } from "next/server";

const MUSIC_URL = process.env.MUSIC_SERVICE_URL || "http://127.0.0.1:8088";

export async function proxyGet(path: string, searchParams?: URLSearchParams) {
  try {
    const qs = searchParams?.toString();
    const url = `${MUSIC_URL}${path}${qs ? `?${qs}` : ""}`;
    const res = await fetch(url);
    const data = await res.json();
    return NextResponse.json(data, { status: res.status });
  } catch {
    return NextResponse.json({ error: "Music service unavailable" }, { status: 502 });
  }
}

export async function proxyMutate(method: string, path: string, body?: any) {
  try {
    const res = await fetch(`${MUSIC_URL}${path}`, {
      method,
      headers: { "Content-Type": "application/json" },
      body: body ? JSON.stringify(body) : undefined,
    });
    const data = await res.json();
    return NextResponse.json(data, { status: res.status });
  } catch {
    return NextResponse.json({ error: "Music service unavailable" }, { status: 502 });
  }
}
