'use server';

import { prisma } from './prisma';

export async function getProducts() {
  return await prisma.product.findMany({ include: { category: true } });
}

export async function getCategories() {
  const categories = await prisma.category.findMany({
    include: {
      _count: {
        select: { products: true }
      }
    }
  });

  return categories.map(c => ({
    id: c.id,
    name: c.name,
    nameAr: c.nameAr,
    icon: c.icon,
    count: c._count.products
  }));
}

export async function getFlashDeals() {
  // Mock logic to get flash deals based on a discount condition
  return await prisma.product.findMany({
    take: 4,
    orderBy: { createdAt: 'desc' },
    include: { category: true }
  });
}

export async function getFeaturedProducts() {
  return await prisma.product.findMany({
    take: 6,
    orderBy: { stock: 'desc' },
    include: { category: true }
  });
}
