'use server';

import { prisma } from '@/lib/prisma';
import { isAdminAuthenticated } from '@/lib/adminAuth';

export async function getReportData() {
  const isAuthenticated = await isAdminAuthenticated();
  if (!isAuthenticated) throw new Error('Unauthorized');

  const [orders, manualSales, expenses, purchases] = await Promise.all([
    prisma.order.findMany({
      select: { id: true, total: true, createdAt: true },
      orderBy: { createdAt: 'asc' },
    }),
    prisma.manualSale.findMany({
      select: { id: true, total: true, soldAt: true, createdAt: true },
      orderBy: { soldAt: 'desc' },
    }),
    prisma.expense.findMany({
      select: { id: true, amount: true, date: true, category: true },
      orderBy: { date: 'desc' },
    }),
    prisma.purchase.findMany({
      select: { id: true, total: true, purchasedAt: true },
      orderBy: { purchasedAt: 'desc' },
    }),
  ]);

  return { orders, manualSales, expenses, purchases };
}
