import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  console.log('🌱 Datenbank wird mit Testdaten befüllt...\n');

  // 1. Benutzer erstellen (Lehrer und Schüler)
  console.log('📝 Erstelle Benutzer...');
  const admin = await prisma.user.upsert({
    where: { email: 'admin@smartesklassenzimmer.de' },
    update: {},
    create: {
      email: 'admin@smartesklassenzimmer.de',
      username: 'admin',
      password: '$2b$10$8I9F3DPBvqzm.tBz7yMgfur4z1gMyaHsPyhmL4VAGJ2OvnmTTn5KW', // admin123
      role: 'Lehrer',
    },
  });

  const teacher1 = await prisma.user.upsert({
    where: { email: 'mueller@schule.de' },
    update: {},
    create: {
      email: 'mueller@schule.de',
      username: 'Frau Müller',
      password: '$2b$10$8I9F3DPBvqzm.tBz7yMgfur4z1gMyaHsPyhmL4VAGJ2OvnmTTn5KW', // admin123
      role: 'Lehrer',
    },
  });

  const teacher2 = await prisma.user.upsert({
    where: { email: 'schmidt@schule.de' },
    update: {},
    create: {
      email: 'schmidt@schule.de',
      username: 'Herr Schmidt',
      password: '$2b$10$8I9F3DPBvqzm.tBz7yMgfur4z1gMyaHsPyhmL4VAGJ2OvnmTTn5KW', // admin123
      role: 'Lehrer',
    },
  });

  const student1 = await prisma.user.upsert({
    where: { email: 'max@student.de' },
    update: {},
    create: {
      email: 'max@student.de',
      username: 'Max Mustermann',
      password: '$2b$10$8I9F3DPBvqzm.tBz7yMgfur4z1gMyaHsPyhmL4VAGJ2OvnmTTn5KW', // admin123
      role: 'Student',
    },
  });

  const student2 = await prisma.user.upsert({
    where: { email: 'anna@student.de' },
    update: {},
    create: {
      email: 'anna@student.de',
      username: 'Anna Schmidt',
      password: '$2b$10$8I9F3DPBvqzm.tBz7yMgfur4z1gMyaHsPyhmL4VAGJ2OvnmTTn5KW', // admin123
      role: 'Student',
    },
  });

  console.log('✅ Benutzer erstellt\n');

  // 2. Fächer erstellen
  console.log('📚 Erstelle Fächer...');
  const mathSubject = await prisma.subject.upsert({
    where: { name: 'Mathematik' },
    update: {},
    create: {
      name: 'Mathematik',
      abbreviation: 'Mathe',
      color: '#FF5733',
    },
  });

  const germanSubject = await prisma.subject.upsert({
    where: { name: 'Deutsch' },
    update: {},
    create: {
      name: 'Deutsch',
      abbreviation: 'DE',
      color: '#33FF57',
    },
  });

  const englishSubject = await prisma.subject.upsert({
    where: { name: 'Englisch' },
    update: {},
    create: {
      name: 'Englisch',
      abbreviation: 'EN',
      color: '#3357FF',
    },
  });

  const physicsSubject = await prisma.subject.upsert({
    where: { name: 'Physik' },
    update: {},
    create: {
      name: 'Physik',
      abbreviation: 'Phy',
      color: '#FF33F5',
    },
  });

  console.log('✅ Fächer erstellt\n');

  // 3. Räume erstellen
  console.log('🏫 Erstelle Räume...');
  const roomA101 = await prisma.room.upsert({
    where: { number: 'A101' },
    update: {},
    create: {
      number: 'A101',
      building: 'Hauptgebäude',
      capacity: 30,
      equipment: 'Beamer, Whiteboard',
    },
  });

  const roomB203 = await prisma.room.upsert({
    where: { number: 'B203' },
    update: {},
    create: {
      number: 'B203',
      building: 'Neubau',
      capacity: 25,
      equipment: 'Smartboard, Laptop',
    },
  });

  const roomC105 = await prisma.room.upsert({
    where: { number: 'C105' },
    update: {},
    create: {
      number: 'C105',
      building: 'Hauptgebäude',
      capacity: 20,
      equipment: 'Beamer',
    },
  });

  console.log('✅ Räume erstellt\n');

  // 4. Stundenplan-Einträge erstellen
  console.log('📅 Erstelle Stundenplan-Einträge...');

  // Montag
  await prisma.timetableEntry.create({
    data: {
      dayOfWeek: 1,
      startTime: '08:00',
      endTime: '09:30',
      subjectId: mathSubject.id,
      teacherId: teacher1.id,
      roomId: roomA101.id,
      isRecurring: true,
    },
  });

  await prisma.timetableEntry.create({
    data: {
      dayOfWeek: 1,
      startTime: '10:00',
      endTime: '11:30',
      subjectId: germanSubject.id,
      teacherId: teacher2.id,
      roomId: roomB203.id,
      isRecurring: true,
    },
  });

  // Dienstag
  await prisma.timetableEntry.create({
    data: {
      dayOfWeek: 2,
      startTime: '08:00',
      endTime: '09:30',
      subjectId: englishSubject.id,
      teacherId: teacher1.id,
      roomId: roomC105.id,
      isRecurring: true,
    },
  });

  await prisma.timetableEntry.create({
    data: {
      dayOfWeek: 2,
      startTime: '10:00',
      endTime: '11:30',
      subjectId: physicsSubject.id,
      teacherId: teacher2.id,
      roomId: roomA101.id,
      isRecurring: true,
    },
  });

  // Mittwoch
  await prisma.timetableEntry.create({
    data: {
      dayOfWeek: 3,
      startTime: '08:00',
      endTime: '09:30',
      subjectId: mathSubject.id,
      teacherId: teacher1.id,
      roomId: roomA101.id,
      isRecurring: true,
    },
  });

  console.log('✅ Stundenplan-Einträge erstellt\n');

  console.log('\n=== LOGIN-DATEN ===');
  console.log('👨‍🏫 Lehrer:');
  console.log('  - Benutzername: admin, Passwort: admin123');
  console.log('  - Benutzername: Frau Müller, Passwort: admin123');
  console.log('  - Benutzername: Herr Schmidt, Passwort: admin123');
  console.log('\n👨‍🎓 Schüler:');
  console.log('  - Benutzername: Max Mustermann, Passwort: admin123');
  console.log('  - Benutzername: Anna Schmidt, Passwort: admin123');
  console.log('==================\n');

  console.log('🎉 Datenbank erfolgreich mit Testdaten befüllt!\n');
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
