import { NextResponse } from "next/server"
import { executeQuery } from "@/lib/database"

export async function GET() {
  try {
    const results = (await executeQuery(`
      SELECT 
        SUM(CASE WHEN in_shelter IS NULL THEN 1 ELSE 0 END) as no_report,
        SUM(CASE WHEN in_shelter = 'yes' THEN 1 ELSE 0 END) as in_shelter,
        SUM(CASE WHEN in_shelter = 'no' THEN 1 ELSE 0 END) as not_in_shelter,
        SUM(CASE WHEN in_shelter = 'no_alarm' THEN 1 ELSE 0 END) as no_alarm,
        SUM(CASE WHEN in_shelter = 'safe_after_exit' THEN 1 ELSE 0 END) as safe_after_exit
      FROM users
    `)) as any[]

    return NextResponse.json(results[0])
  } catch (error) {
    console.error("Stats error:", error)
    return NextResponse.json({ error: "שגיאה בטעינת סטטיסטיקות" }, { status: 500 })
  }
}
