/* Supplemental seeder for the Competition Manager demo:
 *  two clubs (English Language Club, Debating Club) for the primary demo
 *  institution plus one completed & published competition with a point table,
 *  one upcoming competition, and one standalone running event.
 *
 *  Idempotent — skips when the clubs already exist. Runs standalone
 *  (`npx tsx prisma/seed-competitions.ts`) and is also called by the main seed.
 */
import { PrismaClient } from "@prisma/client";

type Prisma = PrismaClient;

export async function seedCompetitions(prisma: Prisma) {
  const inst = await prisma.institution.findUnique({ where: { code: "PS-1004" } });
  if (!inst) return;

  const existing = await prisma.club.findFirst({
    where: { institutionId: inst.id, name: "English Language Club" },
    select: { id: true },
  });
  if (existing) return;

  const students = await prisma.student.findMany({
    where: { institutionId: inst.id },
    orderBy: { roll: "asc" },
    select: { id: true, roll: true },
    take: 5,
  });
  if (students.length < 3) return;

  const english = await prisma.club.create({
    data: {
      institutionId: inst.id,
      name: "English Language Club",
      nameBn: "ইংলিশ ল্যাঙ্গুয়েজ ক্লাব",
      description: "Weekly language practice, spelling bee and public speaking.",
    },
  });
  const debating = await prisma.club.create({
    data: {
      institutionId: inst.id,
      name: "Debating Club",
      nameBn: "ডিবেটিং ক্লাব",
      description: "Inter-class debates and wall magazine competitions.",
    },
  });

  const publishedAt = new Date();
  const debate = await prisma.competition.create({
    data: {
      institutionId: inst.id,
      clubId: debating.id,
      name: "Inter-class Debate 2026",
      nameBn: "আন্তঃশ্রেণি বিতর্ক ২০২৬",
      venue: "Auditorium",
      scheduledAt: new Date("2026-08-20T10:00:00Z"),
      status: "COMPLETED",
    },
  });
  const pointsTable = [
    { rank: 1, points: 10 },
    { rank: 2, points: 7 },
    { rank: 3, points: 5 },
    { rank: 4, points: 3 },
    { rank: 5, points: 1 },
  ];
  for (let i = 0; i < Math.min(students.length, pointsTable.length); i++) {
    await prisma.competitionResult.create({
      data: {
        competitionId: debate.id,
        studentId: students[i].id,
        rank: pointsTable[i].rank,
        points: pointsTable[i].points,
        publishedAt,
      },
    });
  }

  await prisma.competition.create({
    data: {
      institutionId: inst.id,
      clubId: english.id,
      name: "Spelling Bee 2026",
      nameBn: "স্পেলিং বি ২০২৬",
      venue: "Hall Room",
      scheduledAt: new Date("2026-10-15T09:30:00Z"),
      status: "UPCOMING",
    },
  });
  await prisma.competition.create({
    data: {
      institutionId: inst.id,
      name: "Science Olympiad",
      nameBn: "সায়েন্স অলিম্পিয়াড",
      venue: "Science Building",
      scheduledAt: new Date("2026-09-25T10:00:00Z"),
      status: "RUNNING",
    },
  });

  console.log("  ✔ competition manager demo (2 clubs, 3 competitions, published results)");
}

/* standalone entry */
if (process.argv[1]?.includes("seed-competitions")) {
  const prisma = new PrismaClient();
  seedCompetitions(prisma)
    .catch((e) => {
      console.error(e);
      process.exitCode = 1;
    })
    .finally(() => prisma.$disconnect());
}
