import { NextResponse } from 'next/server';
import fs from 'fs';
import path from 'path';

export async function GET(request: Request) {
  const { searchParams } = new URL(request.url);
  const seccion = searchParams.get('seccion');

  try {
    const filePath = path.join(process.cwd(), 'public', 'maps', 'municipio8.json');
    const fileData = fs.readFileSync(filePath, 'utf8');
    const geojson = JSON.parse(fileData);

    if (seccion) {
      const filtered = {
        ...geojson,
        features: geojson.features.filter(
          (f: any) => f.properties.seccion === parseInt(seccion)
        )
      };
      return NextResponse.json(filtered);
    }

    return NextResponse.json(geojson);
  } catch (error) {
    return NextResponse.json({ error: 'Error al cargar mapa' }, { status: 500 });
  }
}