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

// Confirmed live: POST /user/wallet/transactions is real (a GET 404s at
// the Apache level, same "reads like a GET, is actually a POST"
// mislabeling this codebase has hit before on other endpoints) and
// takes {page, limit} — no server-side type/transactionType filter
// actually narrows the result (confirmed live, passing either was a
// no-op), so per-tab filtering (call/chat/payment/other) happens
// client-side against each row's own transactionType/isCredit.
const API_BASE = "https://api.astrology.togwe.com/api/user/wallet/transactions";

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

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

    const body = await request.json().catch(() => ({}));

    const response = await fetch(API_BASE, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
        Authorization: authorization,
      },
      body: JSON.stringify(body),
      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 the wallet API." },
      { status: 500 },
    );
  }
}
