import { NextRequest, NextResponse } from "next/server";

const BRIDGE_URL = process.env.IMESSAGE_BRIDGE_URL || "http://127.0.0.1:8082";

export async function GET(request: NextRequest) {
  const contact = request.nextUrl.searchParams.get("contact");
  const limit = request.nextUrl.searchParams.get("limit") || "50";
  const offset = request.nextUrl.searchParams.get("offset") || "0";

  const params = new URLSearchParams({ limit, offset });
  if (contact) params.set("contact", contact);

  try {
    const res = await fetch(`${BRIDGE_URL}/history?${params}`);
    const data = await res.json();
    return NextResponse.json(data);
  } catch {
    return NextResponse.json(
      { error: "Bridge service unavailable" },
      { status: 502 }
    );
  }
}
