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

const API_BASE = "https://api.astrology.togwe.com/api";

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

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

    const searchParams = request.nextUrl.searchParams;

    const page = searchParams.get("page") || "1";
    const limit = searchParams.get("limit") || "10";
    const type = searchParams.get("type") || "ALL";

    const url =
      `${API_BASE}/astrologer/wallet/transactions` +
      `?page=${encodeURIComponent(page)}` +
      `&limit=${encodeURIComponent(limit)}` +
      `&type=${encodeURIComponent(type)}`;

    const response = await fetch(url, {
      method: "GET",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: authorization,
      },
      cache: "no-store",
    });

    const data = await response.json();

    return NextResponse.json(data, {
      status: response.status,
    });
  } catch (error) {
    console.error("Wallet transactions proxy error:", error);

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