import type { NextApiRequest, NextApiResponse } from "next" import sqlite3 from "sqlite3" import path from "path" export default async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method !== "GET") return res.status(405).json({ error: "Method not allowed" }) const dbPath = path.join(process.cwd(), "database", "antihoax.db") const db = new sqlite3.Database(dbPath) try { // Get performance metrics const stats = await new Promise((resolve, reject) => { db.get(` SELECT COUNT(*) as total_sources, COUNT(CASE WHEN status = 'verified' THEN 1 END) as verified_sources, COUNT(CASE WHEN risk_level >= 4 THEN 1 END) as high_risk_sources, COUNT(CASE WHEN created_at >= date('now', '-7 days') THEN 1 END) as sources_last_week FROM sources `, (err, row) => { if (err) reject(err) else resolve(row) }) }) // Get API usage simulation const apiUsage = { daily_requests: Math.floor(Math.random() * 10000) + 5000, avg_response_time: Math.floor(Math.random() * 150) + 50, error_rate: Math.random() * 0.05 } res.json({ database_stats: stats, api_performance: apiUsage, last_updated: new Date().toISOString() }) } catch (error) { console.error('Analytics error:', error) res.status(500).json({ error: "Failed to fetch analytics" }) } finally { db.close() } }