import { NextRequest, NextResponse } from next/server;
import { requireAuth } from @/lib/auth;

const COMMUNITY_URL = process.env.COMMUNITY_URL || process.env.UMMAHID_URL || "http://ummahid:3000";

export async function POST(req: NextRequest) {
  const jwtPayload = await requireAuth(req);
  if (!jwtPayload) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const auth = req.headers.get("authorization");
  if (!auth) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  try {
    const body = await req.json();
    const r = await fetch(`${COMMUNITY_URL}/api/v2/hibah/accept`, {
      method: "POST", headers: {"Content-Type":"application/json",Authorization:auth}, body: JSON.stringify(body)
    });
    return NextResponse.json(await r.json(), { status: r.status });
  } catch (e) { return NextResponse.json({ error:"Failed" }, { status:502 }); }
}
