import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";

function haversine(lat1: number, lon1: number, lat2: number, lon2: number) {
  const toRad = (v: number) => (v * Math.PI) / 180;
  const R = 6371; // km
  const dLat = toRad(lat2 - lat1);
  const dLon = toRad(lon2 - lon1);
  const a = Math.sin(dLat / 2) ** 2 + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) ** 2;
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  return R * c;
}

export async function GET(req: Request) {
  const { searchParams } = new URL(req.url);
  const lat = Number(searchParams.get("lat"));
  const lon = Number(searchParams.get("lon"));
  const category = searchParams.get("category") || undefined;
  const radius = Number(searchParams.get("radius")) || 10; // km
  const where = category ? { category } : {};
  const partners = await prisma.partner.findMany({ where, orderBy: { createdAt: "desc" } });
  const withDistance = isFinite(lat) && isFinite(lon)
    ? partners.map((p) => ({ ...p, distance: haversine(lat, lon, p.latitude, p.longitude) })).filter((p) => p.distance <= radius)
    : partners.map((p) => ({ ...p, distance: null }));
  withDistance.sort((a, b) => (a.distance ?? 0) - (b.distance ?? 0));
  return NextResponse.json({ partners: withDistance });
}


