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 authHeader = req.headers.get(authorization);
  if (!authHeader) {
    return NextResponse.json({ error: Authorization required }, { status: 401 });
  }
  try {
    const body = await req.json();
    const ummahRes = await fetch(`${COMMUNITY_URL}/api/v2/sadaqah/give`, {
      method: "POST",
      headers: { "Content-Type": "application/json", Authorization: authHeader },
      body: JSON.stringify(body),
    });
    const data = await ummahRes.json();
    return NextResponse.json(data, { status: ummahRes.status });
  } catch (e) {
    console.error("Sadaqah send error:", e);
    return NextResponse.json({ error: "Sadaqah request failed" }, { status: 502 });
  }
}
