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

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

const allowedTypes = [
  "languages",
  "skills",
  "document-types",
  "device-types",
  "working-hours",
];

export async function GET(
  request: NextRequest,
  context: { params: Promise<{ type: string }> }
) {
  try {
    const { type } = await context.params;

    if (!allowedTypes.includes(type)) {
      return NextResponse.json(
        { status: false, message: "Invalid master data type." },
        { status: 400 },
      );
    }

    const authorization = request.headers.get("authorization");

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

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

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