/**
 * Create Test Server Script
 * Adds a test server to the database for testing the server browser
 */

const mongoose = require('mongoose');
const GameServer = require('../models/GameServer');
require('dotenv').config();

async function createTestServer() {
  try {
    // Connect to database
    await mongoose.connect(process.env.MONGODB_URI);
    console.log('Connected to database');

    // Check if test server already exists
    const existingServer = await GameServer.findOne({ serverId: 'test_server_001' });
    if (existingServer) {
      console.log('Test server already exists, deleting it first...');
      await GameServer.deleteOne({ serverId: 'test_server_001' });
    }

    // Create test server
    const testServer = new GameServer({
      serverId: 'test_server_001',
      name: 'Test Server - Galaxy Strike',
      type: 'public',
      region: 'us-east',
      maxPlayers: 10,
      currentPlayers: 2,
      owner: {
        userId: 'test_user_001',
        username: 'TestAdmin'
      },
      settings: {
        description: 'A test server for Galaxy Strike Online',
        tags: ['test', 'beginner', 'pve']
      },
      status: 'active',
      gameServerUrl: 'https://api.korvarix.com'
    });

    await testServer.save();
    console.log('Test server created successfully!');
    console.log('Server details:', {
      id: testServer.serverId,
      name: testServer.name,
      type: testServer.type,
      region: testServer.region,
      currentPlayers: testServer.currentPlayers,
      maxPlayers: testServer.maxPlayers,
      status: testServer.status
    });

  } catch (error) {
    console.error('Error creating test server:', error);
  } finally {
    await mongoose.disconnect();
  }
}

// Run the script
if (require.main === module) {
  createTestServer();
}

module.exports = createTestServer;
