import axios from 'axios';

const JINGROW_SERVER_URL = process.env.JINGROW_SERVER_URL;

export async function GET(request) {
  try {
    const { searchParams } = new URL(request.url);
    const pagetype = searchParams.get('pagetype');
    const name = searchParams.get('name');
    if (!pagetype || !name) {
      return Response.json({ error: 'Missing pagetype or name parameter' }, { status: 400 });
    }

    const response = await axios.get(
      `${JINGROW_SERVER_URL}/api/method/jsite.api.v1.get_category`,
      { params: { pagetype, name } }
    );
    return Response.json(response.data, { status: response.status });
  } catch (error) {
    return Response.json(
      { error: error.message, detail: error?.response?.data || null },
      { status: 500 }
    );
  }
}
