// scripts/seed-test-drones.ts
// Seed mock drone registrations for testing

import mongoose from 'mongoose';
import { DroneRegistration } from '../src/models/drone-registration.js';

async function seedTestDrones() {
  try {
    await mongoose.connect('mongodb://localhost:27017/gadget-code');
    console.log('Connected to MongoDB');

    // Find the test user
    const User = (await import('../src/models/user.js')).default;
    const testUser = await User.findOne({ email_lc: 'rob@digitaltelepresence.com' });
    
    if (!testUser) {
      console.error('Test user not found');
      process.exit(1);
    }

    console.log(`Found test user: ${testUser.email}`);

    // Create mock drones
    const mockDrones = [
      {
        hostname: 'drone-alpha',
        workspaceDir: '/home/rob/workspaces/drone-alpha',
        status: 'available',
        workspaceId: 'workspace-alpha-001',
      },
      {
        hostname: 'drone-beta',
        workspaceDir: '/home/rob/workspaces/drone-beta',
        status: 'available',
        workspaceId: 'workspace-beta-002',
      },
      {
        hostname: 'drone-gamma',
        workspaceDir: '/home/rob/workspaces/drone-gamma',
        status: 'busy',
        workspaceId: 'workspace-gamma-003',
      },
      {
        hostname: 'drone-offline-1',
        workspaceDir: '/home/rob/workspaces/drone-offline-1',
        status: 'offline',
        workspaceId: 'workspace-offline-004',
      },
      {
        hostname: 'drone-offline-2',
        workspaceDir: '/home/rob/workspaces/drone-offline-2',
        status: 'offline',
        workspaceId: 'workspace-offline-005',
      },
    ];

    // Delete existing test drones
    await DroneRegistration.deleteMany({
      hostname: { $in: mockDrones.map(d => d.hostname) },
      user: testUser._id,
    });
    console.log('Cleared existing test drones');

    // Create new mock drones
    const created = [];
    for (const droneData of mockDrones) {
      const drone = new DroneRegistration({
        ...droneData,
        user: testUser._id,
        createdAt: new Date(),
        updatedAt: new Date(),
      });
      await drone.save();
      created.push(drone);
      console.log(`Created drone: ${drone.hostname} (${drone.status})`);
    }

    console.log(`\n✅ Seeded ${created.length} test drones`);
    console.log('  - 2 available drones');
    console.log('  - 1 busy drone');
    console.log('  - 2 offline drones');

    await mongoose.disconnect();
    process.exit(0);
  } catch (error) {
    console.error('Error seeding drones:', error);
    process.exit(1);
  }
}

seedTestDrones();
