import { NextResponse } from 'next/server';
import { getAnalytics, getOrders, getUsers, getReviews } from '@/lib/db';

export async function GET() {
  const analytics = getAnalytics();
  const orders = getOrders();
  const users = getUsers();
  const reviews = getReviews();

  const totalRevenue = orders.filter(o => o.paymentStatus === 'paid').reduce((s, o) => s + o.total, 0);
  const totalOrders = orders.length;
  const totalUsers = users.filter(u => u.role === 'customer').length;
  const totalReviews = reviews.length;
  const avgRating = reviews.length
    ? (reviews.reduce((s, r) => s + r.rating, 0) / reviews.length).toFixed(1)
    : '0';

  return NextResponse.json({
    success: true,
    summary: { totalRevenue, totalOrders, totalUsers, totalReviews, avgRating },
    daily: analytics,
  });
}
