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

// Confirmed live (real Postman capture): returns every consultation
// that has reached a settlement stage, with `pendingAmount` /
// `toBeSettledAmount` / `onHoldAmount` / `settledAmount` / `totalEarning`
// summary fields alongside the paginated `items` list.
const API_BASE = "https://api.astrology.togwe.com/api/astrologer/settlement/consultations";

export async function GET(request: NextRequest) {
  try {
    const authorization = request.headers.get("authorization");

    if (!authorization) {
      return NextResponse.json(
        { status: false, message: "Authentication token is required." },
        { status: 401 },
      );
    }

    const search = request.nextUrl.searchParams.toString();

    const response = await fetch(`${API_BASE}${search ? `?${search}` : ""}`, {
      method: "GET",
      headers: {
        Accept: "application/json",
        Authorization: authorization,
      },
      cache: "no-store",
    });

    const data = await response.json();

    return NextResponse.json(data, { status: response.status });
  } catch (error) {
    console.error("Astrologer settlement consultations proxy error:", error);

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