File size: 1,855 Bytes
78d0e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { NextResponse } from "next/server"
import { DatabaseService } from "@/lib/database"

export async function GET() {
  try {
    // Get all users to calculate statistics
    const allUsers = await DatabaseService.getAllUsers()

    // Calculate verified CHWs (Community Health Workers)
    const verifiedCHWs = allUsers.filter(
      (user) => user.raw_json.type === "healer" && user.raw_json.verificationStatus === "verified",
    ).length

    // Calculate active Guardians
    const activeGuardians = allUsers.filter((user) => user.raw_json.type === "guardian").length

    // Calculate total support (sum of all Guardian contributions)
    const totalSupport = allUsers
      .filter((user) => user.raw_json.type === "guardian")
      .reduce((sum, user) => {
        const contribution = user.raw_json.contributionAmount
        const amount = typeof contribution === "string" ? Number.parseFloat(contribution) : contribution
        return sum + (amount || 0)
      }, 0)

    // Calculate lives impacted (sum of patients served by verified healers)
    const livesImpacted = allUsers
      .filter((user) => user.raw_json.type === "healer" && user.raw_json.verificationStatus === "verified")
      .reduce((sum, user) => {
        return sum + (user.raw_json.impact?.patientsServed || 0)
      }, 0)

    // Total users count
    const totalUsers = allUsers.length

    const stats = {
      verifiedCHWs,
      activeGuardians,
      totalSupport,
      livesImpacted,
      totalUsers,
      lastUpdated: new Date().toISOString(),
    }

    return NextResponse.json(stats, {
      headers: {
        "Cache-Control": "s-maxage=60, stale-while-revalidate=300",
      },
    })
  } catch (error) {
    console.error("Error fetching stats:", error)
    return NextResponse.json({ error: "Failed to fetch statistics" }, { status: 500 })
  }
}