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

// Confirmed live: one settlement batch plus `consultations[]` — every
// session paid out in it, with its own gross/fee/earning/status.
const API_BASE = "https://api.astrology.togwe.com/api/astrologer/settlement/history";

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> },
) {
  try {
    const authorization = request.headers.get("authorization");

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

    const { id } = await params;

    if (!/^\d+$/.test(id)) {
      return NextResponse.json(
        { status: false, message: "Invalid settlement id." },
        { status: 400 },
      );
    }

    const response = await fetch(`${API_BASE}/${id}`, {
      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 detail proxy error:", error);

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