"use server";

import { prisma } from '@/lib/prisma';
import { revalidatePath } from 'next/cache';

export async function getReports() {
  return await prisma.messageReport.findMany({
    include: {
        message: {
            include: {
                sender: {
                    select: { id: true, name: true, email: true }
                },
                conversation: {
                    include: {
                        business: {
                            select: { id: true, name: true }
                        },
                        messages: {
                            orderBy: { createdAt: 'asc' },
                            include: {
                                sender: {
                                    select: { id: true, name: true }
                                }
                            }
                        }
                    }
                }
            }
        },
        reporter: {
            select: { id: true, name: true, email: true }
        }
    },
    orderBy: {
      createdAt: 'desc',
    },
  });
}

export async function updateReportStatus(id: string, status: 'RESOLVED' | 'DISMISSED') {
  try {
    await prisma.messageReport.update({
      where: { id },
      data: { status }
    });
    revalidatePath('/moderation');
    return { success: true };
  } catch (error) {
    return { success: false, error: "Erreur lors de la mise à jour" };
  }
}
