import { NextResponse } from 'next/server';
import connectToDatabase from '@/lib/mongodb';
import ScheduleBlock from '@/models/ScheduleBlock';

const colors: Record<string, string> = {
  sleep: '#64748b',
  meal: '#f59e0b',
  bus: '#ef4444',
  course: '#3b82f6',
  work: '#10b981',
  webdev: '#a855f7',
  weekend: '#f97316',
  free: '#14b8a6',
};

export async function GET() {
  try {
    await connectToDatabase();
    await ScheduleBlock.deleteMany({});

    const blocks: { dayOfWeek: string; startTime: string; endTime: string; activityName: string; blockType: string; color: string }[] = [];
    const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];

    // Weekdays common
    weekdays.forEach(day => {
      blocks.push(
        { dayOfWeek: day, startTime: '00:00', endTime: '05:00', activityName: 'නින්ද', blockType: 'sleep', color: colors.sleep },
        { dayOfWeek: day, startTime: '05:00', endTime: '05:30', activityName: 'Wake up / සූදානම', blockType: 'meal', color: colors.meal },
        { dayOfWeek: day, startTime: '05:30', endTime: '08:30', activityName: 'Bus ගමන', blockType: 'bus', color: colors.bus },
        { dayOfWeek: day, startTime: '08:30', endTime: '16:00', activityName: 'NVQ L4 Electrical Course', blockType: 'course', color: colors.course },
        { dayOfWeek: day, startTime: '16:00', endTime: '19:30', activityName: 'Bus ගමන', blockType: 'bus', color: colors.bus }
      );
    });

    // Mon, Wed, Thu evenings
    ['Monday', 'Wednesday', 'Thursday'].forEach(day => {
      blocks.push(
        { dayOfWeek: day, startTime: '19:30', endTime: '20:00', activityName: 'රාත්‍රී ආහාර', blockType: 'meal', color: colors.meal },
        { dayOfWeek: day, startTime: '20:00', endTime: '24:00', activityName: 'Online Class Coordinator', blockType: 'work', color: colors.work }
      );
    });

    // Tue, Fri evenings
    ['Tuesday', 'Friday'].forEach(day => {
      blocks.push(
        { dayOfWeek: day, startTime: '19:30', endTime: '20:00', activityName: 'රාත්‍රී ආහාර', blockType: 'meal', color: colors.meal },
        { dayOfWeek: day, startTime: '20:00', endTime: '22:30', activityName: 'Web Dev', blockType: 'webdev', color: colors.webdev },
        { dayOfWeek: day, startTime: '22:30', endTime: '24:00', activityName: 'නින්ද', blockType: 'sleep', color: colors.sleep }
      );
    });

    // Saturday
    blocks.push(
      { dayOfWeek: 'Saturday', startTime: '00:00', endTime: '06:30', activityName: 'sleep', blockType: 'sleep', color: colors.sleep },
      { dayOfWeek: 'Saturday', startTime: '06:30', endTime: '07:30', activityName: 'meal', blockType: 'meal', color: colors.meal },
      { dayOfWeek: 'Saturday', startTime: '07:30', endTime: '15:00', activityName: 'Weekend Class', blockType: 'weekend', color: colors.weekend },
      { dayOfWeek: 'Saturday', startTime: '15:00', endTime: '15:30', activityName: 'meal', blockType: 'meal', color: colors.meal },
      { dayOfWeek: 'Saturday', startTime: '15:30', endTime: '18:00', activityName: 'Web Dev', blockType: 'webdev', color: colors.webdev },
      { dayOfWeek: 'Saturday', startTime: '18:00', endTime: '20:00', activityName: 'විවේකය', blockType: 'free', color: colors.free },
      { dayOfWeek: 'Saturday', startTime: '20:00', endTime: '24:00', activityName: 'sleep', blockType: 'sleep', color: colors.sleep }
    );

    // Sunday
    blocks.push(
      { dayOfWeek: 'Sunday', startTime: '00:00', endTime: '06:30', activityName: 'sleep', blockType: 'sleep', color: colors.sleep },
      { dayOfWeek: 'Sunday', startTime: '06:30', endTime: '07:30', activityName: 'meal', blockType: 'meal', color: colors.meal },
      { dayOfWeek: 'Sunday', startTime: '07:30', endTime: '14:00', activityName: 'Weekend Class', blockType: 'weekend', color: colors.weekend },
      { dayOfWeek: 'Sunday', startTime: '14:00', endTime: '14:30', activityName: 'meal', blockType: 'meal', color: colors.meal },
      { dayOfWeek: 'Sunday', startTime: '14:30', endTime: '17:00', activityName: 'Web Dev', blockType: 'webdev', color: colors.webdev },
      { dayOfWeek: 'Sunday', startTime: '17:00', endTime: '19:30', activityName: 'විවේකය', blockType: 'free', color: colors.free },
      { dayOfWeek: 'Sunday', startTime: '19:30', endTime: '20:00', activityName: 'meal', blockType: 'meal', color: colors.meal },
      { dayOfWeek: 'Sunday', startTime: '20:00', endTime: '24:00', activityName: 'Online Class Coordinator', blockType: 'work', color: colors.work }
    );

    await ScheduleBlock.insertMany(blocks);

    return NextResponse.json({ success: true, count: blocks.length, message: "Successfully seeded the schedule" });
  } catch (error: Error | unknown) {
    return NextResponse.json({ error: error instanceof Error ? error.message : 'Failed to seed data' }, { status: 500 });
  }
}
