import mongoose from 'mongoose';
import Event from '../server/models/event.js';
import dotenv from 'dotenv';

dotenv.config();

const MONGODB_URI = process.env.NUXT_MONGODB_URI;
const SERIES_ID = 'cooperative-values-into-practice'; // From the series collection

const moduleEvents = [
  {
    title: 'Module 0: Orientation',
    slug: 'cooperative-values-module-0-orientation',
    tagline: 'Welcome to Cooperative Values into Practice',
    description: 'An introduction to the series and what to expect',
    content: 'Welcome to the Cooperative Values into Practice series. This orientation module will introduce you to the program structure and learning objectives.',
    startDate: new Date('2025-11-01T18:00:00.000Z'),
    endDate: new Date('2025-11-01T20:00:00.000Z'),
    eventType: 'workshop',
    location: '#workshop-series',
    isOnline: true,
    isVisible: true,
    isCancelled: false,
    membersOnly: false,
    registrationRequired: true,
    maxAttendees: 50,
    targetCircles: [],
    agenda: [],
    speakers: [],
    series: {
      id: SERIES_ID,
      title: 'Cooperative Values into Practice',
      description: 'Creating a democratic and inclusive game studio',
      type: 'course',
      position: 0,
      totalEvents: 6,
      isSeriesEvent: true
    },
    createdBy: 'admin'
  },
  {
    title: 'Module 1: Principles',
    slug: 'cooperative-values-module-1-principles',
    tagline: 'Core principles of cooperative organizations',
    description: 'Learn the foundational principles that guide cooperative businesses',
    content: 'Explore the core principles of cooperative organizations and how they apply to game development studios.',
    startDate: new Date('2025-11-01T18:00:00.000Z'),
    endDate: new Date('2025-11-01T20:00:00.000Z'),
    eventType: 'workshop',
    location: '#workshop-series',
    isOnline: true,
    isVisible: true,
    isCancelled: false,
    membersOnly: false,
    registrationRequired: true,
    maxAttendees: 50,
    targetCircles: [],
    agenda: [],
    speakers: [],
    series: {
      id: SERIES_ID,
      title: 'Cooperative Values into Practice',
      description: 'Creating a democratic and inclusive game studio',
      type: 'course',
      position: 1,
      totalEvents: 6,
      isSeriesEvent: true
    },
    createdBy: 'admin'
  },
  {
    title: 'Module 2: Purpose',
    slug: 'cooperative-values-module-2-purpose',
    tagline: 'Defining your cooperative purpose',
    description: 'Establish the mission and values of your cooperative studio',
    content: 'Learn how to define and articulate your cooperative studio\'s purpose, mission, and core values.',
    startDate: new Date('2025-11-22T18:00:00.000Z'),
    endDate: new Date('2025-11-22T20:00:00.000Z'),
    eventType: 'workshop',
    location: '#workshop-series',
    isOnline: true,
    isVisible: true,
    isCancelled: false,
    membersOnly: false,
    registrationRequired: true,
    maxAttendees: 50,
    targetCircles: [],
    agenda: [],
    speakers: [],
    series: {
      id: SERIES_ID,
      title: 'Cooperative Values into Practice',
      description: 'Creating a democratic and inclusive game studio',
      type: 'course',
      position: 2,
      totalEvents: 6,
      isSeriesEvent: true
    },
    createdBy: 'admin'
  },
  {
    title: 'Module 3: Practices Part 1 - Meetings & Decision-Making',
    slug: 'cooperative-values-module-3-practices-part-1',
    tagline: 'Democratic practices for effective collaboration',
    description: 'Learn meeting facilitation and consensus decision-making processes',
    content: 'Develop practical skills in democratic meeting facilitation and collaborative decision-making.',
    startDate: new Date('2025-12-20T18:00:00.000Z'),
    endDate: new Date('2025-12-20T20:00:00.000Z'),
    eventType: 'workshop',
    location: '#workshop-series',
    isOnline: true,
    isVisible: true,
    isCancelled: false,
    membersOnly: false,
    registrationRequired: true,
    maxAttendees: 50,
    targetCircles: [],
    agenda: [],
    speakers: [],
    series: {
      id: SERIES_ID,
      title: 'Cooperative Values into Practice',
      description: 'Creating a democratic and inclusive game studio',
      type: 'course',
      position: 3,
      totalEvents: 6,
      isSeriesEvent: true
    },
    createdBy: 'admin'
  },
  {
    title: 'Module 4: Practices Part 2 - Finances & Governance',
    slug: 'cooperative-values-module-4-practices-part-2',
    tagline: 'Financial management and governance structures',
    description: 'Explore cooperative financial models and governance frameworks',
    content: 'Learn about cooperative financial management, governance structures, and accountability practices.',
    startDate: new Date('2026-01-17T18:00:00.000Z'),
    endDate: new Date('2026-01-17T20:00:00.000Z'),
    eventType: 'workshop',
    location: '#workshop-series',
    isOnline: true,
    isVisible: true,
    isCancelled: false,
    membersOnly: false,
    registrationRequired: true,
    maxAttendees: 50,
    targetCircles: [],
    agenda: [],
    speakers: [],
    series: {
      id: SERIES_ID,
      title: 'Cooperative Values into Practice',
      description: 'Creating a democratic and inclusive game studio',
      type: 'course',
      position: 4,
      totalEvents: 6,
      isSeriesEvent: true
    },
    createdBy: 'admin'
  },
  {
    title: 'Module 5: Pathways Forward',
    slug: 'cooperative-values-module-5-pathways-forward',
    tagline: 'Creating your action plan',
    description: 'Develop your roadmap for implementing cooperative values',
    content: 'Create a concrete action plan for implementing cooperative values and practices in your studio.',
    startDate: new Date('2026-02-14T18:00:00.000Z'),
    endDate: new Date('2026-02-14T20:00:00.000Z'),
    eventType: 'workshop',
    location: '#workshop-series',
    isOnline: true,
    isVisible: true,
    isCancelled: false,
    membersOnly: false,
    registrationRequired: true,
    maxAttendees: 50,
    targetCircles: [],
    agenda: [],
    speakers: [],
    series: {
      id: SERIES_ID,
      title: 'Cooperative Values into Practice',
      description: 'Creating a democratic and inclusive game studio',
      type: 'course',
      position: 5,
      totalEvents: 6,
      isSeriesEvent: true
    },
    createdBy: 'admin'
  }
];

async function addModuleEvents() {
  try {
    await mongoose.connect(MONGODB_URI);
    console.log('Connected to Atlas');

    // Check if any Module events already exist
    const existing = await Event.find({ title: /^Module/ });
    if (existing.length > 0) {
      console.log(`Found ${existing.length} existing Module events. Deleting them first...`);
      await Event.deleteMany({ title: /^Module/ });
    }

    // Insert the module events
    console.log(`Inserting ${moduleEvents.length} Module events...`);
    const inserted = await Event.insertMany(moduleEvents);
    console.log(`✓ Successfully inserted ${inserted.length} Module events`);

    console.log('\nModule events created:');
    inserted.forEach(e => {
      console.log(`  - ${e.title} (${e.startDate.toISOString().split('T')[0]})`);
    });

    // Verify
    const total = await Event.countDocuments();
    const visible = await Event.countDocuments({ isVisible: true });
    console.log(`\nTotal events in Atlas: ${total}`);
    console.log(`Visible events in Atlas: ${visible}`);

    process.exit(0);
  } catch (error) {
    console.error('Error:', error);
    process.exit(1);
  }
}

addModuleEvents();
