import { NextResponse } from 'next/server';

import { getOverviewStatistics } from '@/services/statistics';

import { reportApiError } from '@/utils/apiError';

// The map drawn from dots needs no more precision than this (about 100 m), and it halves the size.
const round = (value: number) => Math.round(value * 1000) / 1000;

// The places of the creations for "A pontokból kirajzolódik az alkotások földrajzi helye". They are not
// part of the page's payload: the dots come when the view comes near. The Java response comes from
// the fetch cache, so this stays cheap per request.
export async function GET() {
  try {
    const { points } = await getOverviewStatistics();
    return NextResponse.json({ points: points.map(([lng, lat]) => [round(lng), round(lat)]) });
  } catch (error) {
    reportApiError(error, 'Creation points unavailable:');
    return NextResponse.json({ error: 'Creation points unavailable' }, { status: 500 });
  }
}
