/**
 * Debug endpoint: inspect raw Amap transit API response + parsed result.
 * GET /api/debug/transit?olat=31.23&olng=121.47&dlat=31.20&dlng=121.50&city=上海
 * Only available in development.
 */
import { NextRequest, NextResponse } from "next/server";
import { getTransitDirection } from "@/lib/amap";

export async function GET(req: NextRequest) {
  if (process.env.NODE_ENV !== "development") {
    return NextResponse.json({ error: "only available in development" }, { status: 403 });
  }

  const { searchParams } = req.nextUrl;
  const oLat = Number(searchParams.get("olat"));
  const oLng = Number(searchParams.get("olng"));
  const dLat = Number(searchParams.get("dlat"));
  const dLng = Number(searchParams.get("dlng"));
  const city = searchParams.get("city") ?? "上海";

  if (!oLat || !oLng || !dLat || !dLng) {
    return NextResponse.json({ error: "需要 olat/olng/dlat/dlng 参数" }, { status: 400 });
  }

  const result = await getTransitDirection({
    originLat: oLat,
    originLng: oLng,
    destLat: dLat,
    destLng: dLng,
    city,
  });

  if (!result) {
    return NextResponse.json({ error: "未找到路线" });
  }

  return NextResponse.json({ parsed: result });
}
