/**
 * Mock Configuration Presets
 * 
 * Different scenarios for testing the SPORE UI with various conditions
 */

const mockConfigs = {
  // Default healthy cluster
  healthy: {
    name: "Healthy Cluster",
    description: "All nodes active and functioning normally",
    nodes: [
      {
        ip: '192.168.1.100',
        hostname: 'spore-node-1',
        chipId: 12345678,
        status: 'ACTIVE',
        latency: 5
      },
      {
        ip: '192.168.1.101', 
        hostname: 'spore-node-2',
        chipId: 87654321,
        status: 'ACTIVE',
        latency: 8
      },
      {
        ip: '192.168.1.102',
        hostname: 'spore-node-3', 
        chipId: 11223344,
        status: 'ACTIVE',
        latency: 12
      }
    ],
    simulation: {
      enableTimeProgression: true,
      enableRandomFailures: false,
      failureRate: 0.0,
      updateInterval: 5000,
      primaryNodeRotation: false,
      rotationInterval: 30000
    }
  },

  // Single node scenario
  single: {
    name: "Single Node",
    description: "Only one node in the cluster",
    nodes: [
      {
        ip: '192.168.1.100',
        hostname: 'spore-node-1',
        chipId: 12345678,
        status: 'ACTIVE',
        latency: 5
      }
    ],
    simulation: {
      enableTimeProgression: true,
      enableRandomFailures: false,
      failureRate: 0.0,
      updateInterval: 5000,
      primaryNodeRotation: false,
      rotationInterval: 30000
    }
  },

  // Large cluster
  large: {
    name: "Large Cluster",
    description: "Many nodes in the cluster",
    nodes: [
      { ip: '192.168.1.100', hostname: 'spore-node-1', chipId: 12345678, status: 'ACTIVE', latency: 5 },
      { ip: '192.168.1.101', hostname: 'spore-node-2', chipId: 87654321, status: 'ACTIVE', latency: 8 },
      { ip: '192.168.1.102', hostname: 'spore-node-3', chipId: 11223344, status: 'ACTIVE', latency: 12 },
      { ip: '192.168.1.103', hostname: 'spore-node-4', chipId: 44332211, status: 'ACTIVE', latency: 15 },
      { ip: '192.168.1.104', hostname: 'spore-node-5', chipId: 55667788, status: 'ACTIVE', latency: 7 },
      { ip: '192.168.1.105', hostname: 'spore-node-6', chipId: 99887766, status: 'ACTIVE', latency: 20 },
      { ip: '192.168.1.106', hostname: 'spore-node-7', chipId: 11223355, status: 'ACTIVE', latency: 9 },
      { ip: '192.168.1.107', hostname: 'spore-node-8', chipId: 66778899, status: 'ACTIVE', latency: 11 }
    ],
    simulation: {
      enableTimeProgression: true,
      enableRandomFailures: false,
      failureRate: 0.0,
      updateInterval: 5000,
      primaryNodeRotation: true,
      rotationInterval: 30000
    }
  },

  // Degraded cluster with some failures
  degraded: {
    name: "Degraded Cluster",
    description: "Some nodes are inactive or dead",
    nodes: [
      {
        ip: '192.168.1.100',
        hostname: 'spore-node-1',
        chipId: 12345678,
        status: 'ACTIVE',
        latency: 5
      },
      {
        ip: '192.168.1.101', 
        hostname: 'spore-node-2',
        chipId: 87654321,
        status: 'INACTIVE',
        latency: 8
      },
      {
        ip: '192.168.1.102',
        hostname: 'spore-node-3', 
        chipId: 11223344,
        status: 'DEAD',
        latency: 12
      },
      {
        ip: '192.168.1.103',
        hostname: 'spore-node-4', 
        chipId: 44332211,
        status: 'ACTIVE',
        latency: 15
      }
    ],
    simulation: {
      enableTimeProgression: true,
      enableRandomFailures: true,
      failureRate: 0.1,
      updateInterval: 5000,
      primaryNodeRotation: false,
      rotationInterval: 30000
    }
  },

  // High failure rate scenario
  unstable: {
    name: "Unstable Cluster",
    description: "High failure rate with frequent node changes",
    nodes: [
      {
        ip: '192.168.1.100',
        hostname: 'spore-node-1',
        chipId: 12345678,
        status: 'ACTIVE',
        latency: 5
      },
      {
        ip: '192.168.1.101', 
        hostname: 'spore-node-2',
        chipId: 87654321,
        status: 'ACTIVE',
        latency: 8
      },
      {
        ip: '192.168.1.102',
        hostname: 'spore-node-3', 
        chipId: 11223344,
        status: 'ACTIVE',
        latency: 12
      }
    ],
    simulation: {
      enableTimeProgression: true,
      enableRandomFailures: true,
      failureRate: 0.3, // 30% chance of failures
      updateInterval: 2000, // Update every 2 seconds
      primaryNodeRotation: true,
      rotationInterval: 15000 // Rotate every 15 seconds
    }
  },

  // No nodes scenario
  empty: {
    name: "Empty Cluster",
    description: "No nodes discovered",
    nodes: [],
    simulation: {
      enableTimeProgression: false,
      enableRandomFailures: false,
      failureRate: 0.0,
      updateInterval: 5000,
      primaryNodeRotation: false,
      rotationInterval: 30000
    }
  },

  // Development scenario with custom settings
  development: {
    name: "Development Mode",
    description: "Custom settings for development and testing",
    nodes: [
      {
        ip: '192.168.1.100',
        hostname: 'dev-node-1',
        chipId: 12345678,
        status: 'ACTIVE',
        latency: 5
      },
      {
        ip: '192.168.1.101', 
        hostname: 'dev-node-2',
        chipId: 87654321,
        status: 'ACTIVE',
        latency: 8
      }
    ],
    simulation: {
      enableTimeProgression: true,
      enableRandomFailures: true,
      failureRate: 0.05, // 5% failure rate
      updateInterval: 3000, // Update every 3 seconds
      primaryNodeRotation: true,
      rotationInterval: 20000 // Rotate every 20 seconds
    }
  }
};

/**
 * Get a mock configuration by name
 * @param {string} configName - Name of the configuration preset
 * @returns {Object} Mock configuration object
 */
function getMockConfig(configName = 'healthy') {
  const config = mockConfigs[configName];
  if (!config) {
    console.warn(`Unknown mock config: ${configName}. Using 'healthy' instead.`);
    return mockConfigs.healthy;
  }
  return config;
}

/**
 * List all available mock configurations
 * @returns {Array} Array of configuration names and descriptions
 */
function listMockConfigs() {
  return Object.keys(mockConfigs).map(key => ({
    name: key,
    displayName: mockConfigs[key].name,
    description: mockConfigs[key].description,
    nodeCount: mockConfigs[key].nodes.length
  }));
}

/**
 * Create a custom mock configuration
 * @param {Object} options - Configuration options
 * @returns {Object} Custom mock configuration
 */
function createCustomConfig(options = {}) {
  const defaultConfig = {
    name: "Custom Configuration",
    description: "User-defined mock configuration",
    nodes: [
      {
        ip: '192.168.1.100',
        hostname: 'custom-node-1',
        chipId: 12345678,
        status: 'ACTIVE',
        latency: 5
      }
    ],
    simulation: {
      enableTimeProgression: true,
      enableRandomFailures: false,
      failureRate: 0.0,
      updateInterval: 5000,
      primaryNodeRotation: false,
      rotationInterval: 30000
    }
  };

  // Merge with provided options
  return {
    ...defaultConfig,
    ...options,
    nodes: options.nodes || defaultConfig.nodes,
    simulation: {
      ...defaultConfig.simulation,
      ...options.simulation
    }
  };
}

module.exports = {
  mockConfigs,
  getMockConfig,
  listMockConfigs,
  createCustomConfig
};
