import type { WarningDashboard, WarningListQuery } from '@nexumi/shared';
import { prisma } from '../prisma';

export async function listWarnings(guildId: string, query: WarningListQuery): Promise<WarningDashboard[]> {
  const warnings = await prisma.warning.findMany({
    where: {
      guildId,
      ...(query.userId ? { userId: query.userId } : {})
    },
    orderBy: { createdAt: 'desc' },
    take: 100
  });

  return warnings.map((warning) => ({
    id: warning.id,
    userId: warning.userId,
    moderatorId: warning.moderatorId,
    reason: warning.reason,
    caseId: warning.caseId,
    createdAt: warning.createdAt.toISOString()
  }));
}

export async function deleteWarning(guildId: string, warningId: string): Promise<boolean> {
  const existing = await prisma.warning.findFirst({
    where: { id: warningId, guildId }
  });
  if (!existing) {
    return false;
  }
  await prisma.warning.delete({ where: { id: existing.id } });
  return true;
}
