import { getDb } from "../db.js";

export async function GET({ request }) {
  const city = new URL(request.url).searchParams.get("city")?.trim() || "";

  if (!city) {
    return new Response(JSON.stringify({ hasStreets: false }), {
      headers: { "Content-Type": "application/json; charset=utf-8" },
    });
  }

  const db = getDb();

  const row = db
    .prepare(
      `SELECT COUNT(*) AS cnt
       FROM ranges
       WHERE LOWER(city) = LOWER(?)
         AND TRIM(street) <> ''`
    )
    .get(city);

  return new Response(
    JSON.stringify({ hasStreets: row.cnt > 0 }),
    {
      headers: { "Content-Type": "application/json; charset=utf-8" },
    }
  );
}
