import { PrismaClient, Role, Department, ArrivalMode, EventType, EventStatus, VehicleType, VehicleStatus } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  console.log('🌱 Seeding database...');

  // Clean up existing data (careful in production!)
  await prisma.scheduleEvent.deleteMany({});
  await prisma.flight.deleteMany({});
  await prisma.vehicle.deleteMany({});
  await prisma.driver.deleteMany({});
  await prisma.vIP.deleteMany({});
  await prisma.user.deleteMany({});

  console.log('✅ Cleared existing data');

  // Create sample users
  const admin = await prisma.user.create({
    data: {
      auth0Id: 'auth0|admin-sample-id',
      email: 'admin@example.com',
      name: 'Admin User',
      role: Role.ADMINISTRATOR,
      isApproved: true,
    },
  });

  const coordinator = await prisma.user.create({
    data: {
      auth0Id: 'auth0|coordinator-sample-id',
      email: 'coordinator@example.com',
      name: 'Coordinator User',
      role: Role.COORDINATOR,
      isApproved: true,
    },
  });

  // Note: test@test.com user is auto-created and auto-approved on first login (see auth.service.ts)

  console.log('✅ Created sample users');

  // Create sample vehicles with capacity
  const blackSUV = await prisma.vehicle.create({
    data: {
      name: 'Black Suburban',
      type: VehicleType.SUV,
      licensePlate: 'ABC-1234',
      seatCapacity: 6,
      status: VehicleStatus.AVAILABLE,
      notes: 'Leather interior, tinted windows',
    },
  });

  const whiteVan = await prisma.vehicle.create({
    data: {
      name: 'White Sprinter Van',
      type: VehicleType.VAN,
      licensePlate: 'XYZ-5678',
      seatCapacity: 12,
      status: VehicleStatus.AVAILABLE,
      notes: 'High roof, wheelchair accessible',
    },
  });

  const blueSedan = await prisma.vehicle.create({
    data: {
      name: 'Blue Camry',
      type: VehicleType.SEDAN,
      licensePlate: 'DEF-9012',
      seatCapacity: 4,
      status: VehicleStatus.AVAILABLE,
      notes: 'Fuel efficient, good for short trips',
    },
  });

  const grayBus = await prisma.vehicle.create({
    data: {
      name: 'Gray Charter Bus',
      type: VehicleType.BUS,
      licensePlate: 'BUS-0001',
      seatCapacity: 40,
      status: VehicleStatus.AVAILABLE,
      notes: 'Full size charter bus, A/C, luggage compartment',
    },
  });

  console.log('✅ Created sample vehicles with capacities');

  // Create sample drivers
  const driver1 = await prisma.driver.create({
    data: {
      name: 'John Smith',
      phone: '+1 (555) 123-4567',
      department: Department.OFFICE_OF_DEVELOPMENT,
    },
  });

  const driver2 = await prisma.driver.create({
    data: {
      name: 'Jane Doe',
      phone: '+1 (555) 987-6543',
      department: Department.ADMIN,
    },
  });

  const driver3 = await prisma.driver.create({
    data: {
      name: 'Amanda Washington',
      phone: '+1 (555) 234-5678',
      department: Department.OFFICE_OF_DEVELOPMENT,
    },
  });

  const driver4 = await prisma.driver.create({
    data: {
      name: 'Michael Thompson',
      phone: '+1 (555) 876-5432',
      department: Department.ADMIN,
    },
  });

  console.log('✅ Created sample drivers');

  // Create sample VIPs
  const vip1 = await prisma.vIP.create({
    data: {
      name: 'Dr. Robert Johnson',
      organization: 'Tech Corporation',
      department: Department.OFFICE_OF_DEVELOPMENT,
      arrivalMode: ArrivalMode.FLIGHT,
      airportPickup: true,
      venueTransport: true,
      notes: 'Prefers window seat, dietary restriction: vegetarian',
      flights: {
        create: [
          {
            flightNumber: 'AA123',
            flightDate: new Date('2026-02-15'),
            segment: 1,
            departureAirport: 'JFK',
            arrivalAirport: 'LAX',
            scheduledDeparture: new Date('2026-02-15T08:00:00'),
            scheduledArrival: new Date('2026-02-15T11:30:00'),
            status: 'scheduled',
          },
        ],
      },
    },
  });

  const vip2 = await prisma.vIP.create({
    data: {
      name: 'Ms. Sarah Williams',
      organization: 'Global Foundation',
      department: Department.ADMIN,
      arrivalMode: ArrivalMode.SELF_DRIVING,
      expectedArrival: new Date('2026-02-16T14:00:00'),
      airportPickup: false,
      venueTransport: true,
      notes: 'Bringing assistant',
    },
  });

  const vip3 = await prisma.vIP.create({
    data: {
      name: 'Emily Richardson (Harvard University)',
      organization: 'Harvard University',
      department: Department.OFFICE_OF_DEVELOPMENT,
      arrivalMode: ArrivalMode.FLIGHT,
      airportPickup: true,
      venueTransport: true,
      notes: 'Board member, requires accessible vehicle',
    },
  });

  const vip4 = await prisma.vIP.create({
    data: {
      name: 'David Chen (Stanford)',
      organization: 'Stanford University',
      department: Department.OFFICE_OF_DEVELOPMENT,
      arrivalMode: ArrivalMode.FLIGHT,
      airportPickup: true,
      venueTransport: true,
      notes: 'Keynote speaker',
    },
  });

  console.log('✅ Created sample VIPs');

  // Create sample schedule events (unified activities) - NOW WITH MULTIPLE VIPS!

  // Multi-VIP rideshare to Campfire Night (3 VIPs in one SUV)
  await prisma.scheduleEvent.create({
    data: {
      vipIds: [vip3.id, vip4.id, vip1.id], // 3 VIPs sharing a ride
      title: 'Transport to Campfire Night',
      pickupLocation: 'Grand Hotel Lobby',
      dropoffLocation: 'Camp Amphitheater',
      startTime: new Date('2026-02-15T19:45:00'),
      endTime: new Date('2026-02-15T20:00:00'),
      description: 'Rideshare: Emily, David, and Dr. Johnson to campfire',
      type: EventType.TRANSPORT,
      status: EventStatus.SCHEDULED,
      driverId: driver3.id,
      vehicleId: blackSUV.id, // 3 VIPs in 6-seat SUV (3/6 seats used)
    },
  });

  // Single VIP transport
  await prisma.scheduleEvent.create({
    data: {
      vipIds: [vip1.id],
      title: 'Airport Pickup - Dr. Johnson',
      pickupLocation: 'LAX Terminal 4',
      dropoffLocation: 'Grand Hotel',
      startTime: new Date('2026-02-15T11:30:00'),
      endTime: new Date('2026-02-15T12:30:00'),
      description: 'Pick up Dr. Johnson from LAX',
      type: EventType.TRANSPORT,
      status: EventStatus.SCHEDULED,
      driverId: driver1.id,
      vehicleId: blueSedan.id, // 1 VIP in 4-seat sedan (1/4 seats used)
    },
  });

  // Two VIPs sharing lunch transport
  await prisma.scheduleEvent.create({
    data: {
      vipIds: [vip1.id, vip2.id],
      title: 'Transport to Lunch - Day 1',
      pickupLocation: 'Grand Hotel Lobby',
      dropoffLocation: 'Main Dining Hall',
      startTime: new Date('2026-02-15T11:45:00'),
      endTime: new Date('2026-02-15T12:00:00'),
      description: 'Rideshare: Dr. Johnson and Ms. Williams to lunch',
      type: EventType.TRANSPORT,
      status: EventStatus.SCHEDULED,
      driverId: driver2.id,
      vehicleId: blueSedan.id, // 2 VIPs in 4-seat sedan (2/4 seats used)
    },
  });

  // Large group transport in van
  await prisma.scheduleEvent.create({
    data: {
      vipIds: [vip1.id, vip2.id, vip3.id, vip4.id],
      title: 'Morning Shuttle to Conference',
      pickupLocation: 'Grand Hotel Lobby',
      dropoffLocation: 'Conference Center',
      startTime: new Date('2026-02-15T08:00:00'),
      endTime: new Date('2026-02-15T08:30:00'),
      description: 'All VIPs to morning conference session',
      type: EventType.TRANSPORT,
      status: EventStatus.SCHEDULED,
      driverId: driver4.id,
      vehicleId: whiteVan.id, // 4 VIPs in 12-seat van (4/12 seats used)
    },
  });

  // Non-transport activities (unified system)

  // Opening Ceremony - all VIPs attending
  await prisma.scheduleEvent.create({
    data: {
      vipIds: [vip1.id, vip2.id, vip3.id, vip4.id],
      title: 'Opening Ceremony',
      location: 'Main Stage',
      startTime: new Date('2026-02-15T10:00:00'),
      endTime: new Date('2026-02-15T11:30:00'),
      description: 'Welcome and opening remarks',
      type: EventType.EVENT,
      status: EventStatus.SCHEDULED,
    },
  });

  // Lunch - Day 1 (all VIPs)
  await prisma.scheduleEvent.create({
    data: {
      vipIds: [vip1.id, vip2.id, vip3.id, vip4.id],
      title: 'Lunch - Day 1',
      location: 'Main Dining Hall',
      startTime: new Date('2026-02-15T12:00:00'),
      endTime: new Date('2026-02-15T13:30:00'),
      description: 'Day 1 lunch for all attendees',
      type: EventType.MEAL,
      status: EventStatus.SCHEDULED,
    },
  });

  // Campfire Night (all VIPs)
  await prisma.scheduleEvent.create({
    data: {
      vipIds: [vip1.id, vip2.id, vip3.id, vip4.id],
      title: 'Campfire Night',
      location: 'Camp Amphitheater',
      startTime: new Date('2026-02-15T20:00:00'),
      endTime: new Date('2026-02-15T22:00:00'),
      description: 'Evening campfire and networking',
      type: EventType.EVENT,
      status: EventStatus.SCHEDULED,
    },
  });

  // Private meeting - just Dr. Johnson and Ms. Williams
  await prisma.scheduleEvent.create({
    data: {
      vipIds: [vip1.id, vip2.id],
      title: 'Donor Meeting',
      location: 'Conference Room A',
      startTime: new Date('2026-02-15T14:00:00'),
      endTime: new Date('2026-02-15T15:00:00'),
      description: 'Private meeting with development team',
      type: EventType.MEETING,
      status: EventStatus.SCHEDULED,
    },
  });

  console.log('✅ Created sample schedule events with multi-VIP rideshares and activities');

  console.log('\n🎉 Database seeded successfully!');
  console.log('\nSample Users:');
  console.log('- Admin: admin@example.com');
  console.log('- Coordinator: coordinator@example.com');
  console.log('\nSample VIPs:');
  console.log('- Dr. Robert Johnson (Flight arrival)');
  console.log('- Ms. Sarah Williams (Self-driving)');
  console.log('- Emily Richardson (Harvard University)');
  console.log('- David Chen (Stanford)');
  console.log('\nSample Drivers:');
  console.log('- John Smith');
  console.log('- Jane Doe');
  console.log('- Amanda Washington');
  console.log('- Michael Thompson');
  console.log('\nSample Vehicles:');
  console.log('- Black Suburban (SUV, 6 seats)');
  console.log('- White Sprinter Van (Van, 12 seats)');
  console.log('- Blue Camry (Sedan, 4 seats)');
  console.log('- Gray Charter Bus (Bus, 40 seats)');
  console.log('\nSchedule Tasks (Multi-VIP Examples):');
  console.log('- 3 VIPs sharing SUV to Campfire (3/6 seats)');
  console.log('- 2 VIPs sharing sedan to Lunch (2/4 seats)');
  console.log('- 4 VIPs in van to Conference (4/12 seats)');
  console.log('- 1 VIP solo in sedan from Airport (1/4 seats)');
}

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