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

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

    const response =
      await fetch(
        "https://api.astrology.togwe.com/api/user/social/verify-mobile-otp",
        {
          method: "POST",

          headers: {
            "Content-Type":
              "application/json",

            Accept:
              "application/json",
          },

          body: JSON.stringify(body),

          cache: "no-store",
        }
      );

    const text =
      await response.text();

    let data;

    try {
      data = JSON.parse(text);
    } catch {
      console.error(
        "Laravel verify OTP response:",
        text
      );

      return NextResponse.json(
        {
          success: false,
          message:
            "Verify OTP API returned invalid response.",
        },
        {
          status: 502,
        }
      );
    }

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

  } catch (error) {
    console.error(
      "Verify OTP proxy error:",
      error
    );

    return NextResponse.json(
      {
        success: false,
        message:
          "Unable to connect to verify OTP API.",
      },
      {
        status: 500,
      }
    );
  }
}