import { NextResponse } from "next/server";
import { getClientIp } from "@/app/lib/clientIp";

export async function POST(request: Request) {
  try {
    const body = await request.json();

    // Client-sent ip_address is a hardcoded placeholder (the browser
    // has no way to know its own public IP) — override it with the
    // real one as seen by this server, when a reverse proxy in front
    // of it has forwarded it.
    const clientIp = getClientIp(request);
    const payload = clientIp ? { ...body, ip_address: clientIp } : body;

    const apiResponse = await fetch(
      "https://api.astrology.togwe.com/api/user/register",
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(payload),
      }
    );

    const data = await apiResponse.json();

    return NextResponse.json(data, {
      status: apiResponse.status,
    });

  } catch (error) {
    return NextResponse.json(
      {
        success: false,
        message: "Internal Server Error",
      },
      {
        status: 500,
      }
    );
  }
}