import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

const eventNames = [
    "Dentist Appointment",
    "Coffee with Sarah",
    "Quarterly Review",
    "Car Inspection",
    "Therapy Session",
    "Lunch with Bob"
];

const allDayEvents = [
    "Spring Festival",
    "Bank Holiday",
    "Company Offsite",
    "Project Deadline"
];

function getWeekStart(date: Date): Date {
    const d = new Date(date);
    d.setHours(0, 0, 0, 0);
    const day = d.getDay();
    const diff = d.getDate() - day + (day === 0 ? -6 : 1); // Monday is 1
    return new Date(d.setDate(diff));
}

async function main() {
  const user = await prisma.user.findUnique({
    where: { email: 'kugelblitz@gmx.de' }
  });
  
  if (!user) return;

  // 1. Ensure a dummy calendar connection exists
  let connection = await prisma.calendarConnection.findFirst({
      where: { userId: user.id, provider: 'google' }
  });
  if (!connection) {
      connection = await prisma.calendarConnection.create({
          data: {
              userId: user.id,
              provider: 'google',
              accessToken: 'dummy-token',
              calendars: JSON.stringify([{ id: 'primary', name: 'My Calendar', color: '#4285F4' }])
          }
      });
  }

  // 2. Clear old generated mock events
  const mockTitles = [...eventNames, ...allDayEvents];
  await prisma.cachedCalendarEvent.deleteMany({
      where: {
          connectionId: connection.id,
          title: { in: mockTitles }
      }
  });

  const startDate = new Date('2026-03-16T00:00:00Z');
  const endDate = new Date('2026-04-12T00:00:00Z');

  let idCounter = 1;

  for (let d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {
      const dateStr = d.toISOString().split('T')[0];
      const weekStart = getWeekStart(d);

      if (Math.random() > 0.6) {
          // Add All-Day Event
          await prisma.cachedCalendarEvent.create({
              data: {
                  userId: user.id,
                  externalId: `mock-allday-${idCounter++}`,
                  connectionId: connection.id,
                  provider: 'google',
                  calendarId: 'primary',
                  calendarTitle: 'My Calendar',
                  calendarColor: '#primary', // Or any string, frontend handles #
                  title: allDayEvents[Math.floor(Math.random() * allDayEvents.length)],
                  startDate: dateStr,
                  endDate: dateStr,
                  weekStart: weekStart
              }
          });
      }

      if (Math.random() > 0.5) {
          // Add Timed Event
          const hour = Math.floor(Math.random() * 6) + 10; // 10 to 15
          const startDt = new Date(d);
          startDt.setUTCHours(hour, 0, 0, 0);
          const endDt = new Date(d);
          endDt.setUTCHours(hour + 1, 0, 0, 0);

          await prisma.cachedCalendarEvent.create({
              data: {
                  userId: user.id,
                  externalId: `mock-timed-${idCounter++}`,
                  connectionId: connection.id,
                  provider: 'google',
                  calendarId: 'primary',
                  calendarTitle: 'My Calendar',
                  calendarColor: '#EA4335',
                  title: eventNames[Math.floor(Math.random() * eventNames.length)],
                  startDateTime: startDt,
                  endDateTime: endDt,
                  weekStart: weekStart
              }
          });
      }
  }

  console.log("Successfully added simulated external calendar events!");
}

main().catch(console.error).finally(() => prisma.$disconnect());
