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

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

export async function GET() {
  try {
    const res = await fetch(`${BRIDGE_URL}/contacts`);
    const data = await res.json();
    return NextResponse.json(data);
  } catch {
    return NextResponse.json(
      { error: "Bridge service unavailable" },
      { status: 502 }
    );
  }
}

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const res = await fetch(`${BRIDGE_URL}/contacts`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(body),
    });
    const data = await res.json();
    return NextResponse.json(data, { status: 201 });
  } catch {
    return NextResponse.json(
      { error: "Bridge service unavailable" },
      { status: 502 }
    );
  }
}
