/**
 * One-off cleanup: remove appointments, treatments, lab cases, and related rows.
 * Keeps patients, organizations, users, and catalog data intact.
 *
 * Usage: npx ts-node prisma/scripts/clear-clinical-test-data.ts
 */
import { PrismaClient } from '@prisma/client';
import { config } from 'dotenv';
import path from 'path';

config({ path: path.join(__dirname, '..', '..', '.env') });

const prisma = new PrismaClient();

async function main() {
  const counts = {
    labCaseTaskStatusEvents: await prisma.labCaseTaskStatusEvent.count(),
    labCaseComments: await prisma.labCaseComment.count(),
    labCaseAttachments: await prisma.labCaseAttachment.count(),
    labCaseTasks: await prisma.labCaseTask.count(),
    labCaseSends: await prisma.labCaseSend.count(),
    labCaseToothProsthesis: await prisma.labCaseToothProsthesis.count(),
    labCaseDetails: await prisma.labCaseDetail.count(),
    labCases: await prisma.labCase.count(),
    attachments: await prisma.treatmentDetailAttachment.count(),
    treatmentDetails: await prisma.treatmentDetail.count(),
    treatments: await prisma.treatment.count(),
    appointments: await prisma.appointment.count(),
  };

  console.log('Current row counts:', counts);

  await prisma.$transaction([
    prisma.labCaseTaskStatusEvent.deleteMany(),
    prisma.labCaseComment.deleteMany(),
    prisma.labCaseAttachment.deleteMany(),
    prisma.labCaseTask.deleteMany(),
    prisma.labCaseSend.deleteMany(),
    prisma.labCaseToothProsthesis.deleteMany(),
    prisma.labCaseDetail.deleteMany(),
    prisma.labCase.deleteMany(),
    prisma.treatmentDetailAttachment.deleteMany(),
    prisma.treatmentDetail.deleteMany(),
    prisma.treatment.deleteMany(),
    prisma.appointment.deleteMany(),
  ]);

  console.log(
    '✅ Cleared appointments, treatments, lab cases, tasks, comments, attachments, and related rows.',
  );
}

main()
  .catch((error) => {
    console.error('❌ Cleanup failed:', error);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
