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

const API_URL = "https://api.astrology.togwe.com/api/user/astrologer/blocked-list";

export async function POST(request: NextRequest) {
  try {
    const authorization = request.headers.get("authorization");
    if (!authorization) {
      return NextResponse.json({ status: 401, message: "Authorization token is missing." }, { status: 401 });
    }

    const body = await request.json().catch(() => ({}));
    const start = Number.isInteger(Number(body?.start)) && Number(body?.start) >= 0 ? Number(body.start) : 0;
    const requestedLimit = Number(body?.limit);
    const limit = Number.isInteger(requestedLimit) && requestedLimit > 0 ? Math.min(requestedLimit, 100) : 20;

    const upstream = await fetch(API_URL, {
      method: "POST",
      headers: { "Content-Type": "application/json", Accept: "application/json", Authorization: authorization },
      body: JSON.stringify({ start, limit }),
      cache: "no-store",
    });
    const text = await upstream.text();
    let data: unknown;
    try { data = text ? JSON.parse(text) : {}; } catch { data = { message: text }; }
    return NextResponse.json(data, { status: upstream.status });
  } catch (error) {
    console.error("Blocked astrologers list proxy error:", error);
    return NextResponse.json({ status: 500, message: error instanceof Error ? error.message : "Unexpected server error." }, { status: 500 });
  }
}
