/**
 * Test Data Generator Helpers
 * 
 * Reusable utilities for generating category-specific fields and managing field density.
 */

// ============================================================================
// Field Density Logic
// ============================================================================

export type FieldDensity = 'minimal' | 'standard' | 'maximum' | 'mixed';
export type FieldImportance = 'low' | 'medium' | 'high';

/**
 * Determine if a field should be populated based on density and importance
 */
export function shouldPopulateField(
  density: FieldDensity,
  index: number,
  fieldImportance: FieldImportance
): boolean {
  const rand = Math.random();
  
  if (density === 'minimal') {
    return fieldImportance === 'high' ? rand < 0.5 : rand < 0.1;
  } else if (density === 'standard') {
    return fieldImportance === 'high' ? rand < 0.8 : rand < 0.5;
  } else if (density === 'maximum') {
    return rand < 0.95; // Almost everything
  } else if (density === 'mixed') {
    // Vary by entity index
    if (index % 3 === 0) return shouldPopulateField('minimal', index, fieldImportance);
    if (index % 3 === 1) return shouldPopulateField('standard', index, fieldImportance);
    return shouldPopulateField('maximum', index, fieldImportance);
  }
  
  return false;
}

// ============================================================================
// Random Data Generators
// ============================================================================

export function randomInt(min: number, max: number): number {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

export function randomFloat(min: number, max: number, decimals = 2): number {
  const value = Math.random() * (max - min) + min;
  return parseFloat(value.toFixed(decimals));
}

export function randomItem<T>(array: T[]): T {
  return array[randomInt(0, array.length - 1)];
}

export function randomDate(startYear: number, endYear: number): string {
  const year = randomInt(startYear, endYear);
  const month = randomInt(1, 12);
  const day = randomInt(1, 28);
  return `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
}

export function randomDatePrecision(): 'exact' | 'month' | 'year' {
  return randomItem(['exact', 'month', 'year']);
}

// ============================================================================
// Category-Specific Field Generators
// ============================================================================

/**
 * Generate water ride specific fields
 */
export function generateWaterRideFields(density: FieldDensity, index: number) {
  if (!shouldPopulateField(density, index, 'medium')) return {};
  
  return {
    water_depth_cm: randomInt(30, 300),
    splash_height_meters: randomFloat(1, 20, 1),
    wetness_level: randomItem(['dry', 'light', 'moderate', 'soaked']),
    flume_type: randomItem(['log', 'tube', 'raft', 'boat']),
    boat_capacity: randomInt(2, 20)
  };
}

/**
 * Generate dark ride specific fields
 */
export function generateDarkRideFields(density: FieldDensity, index: number) {
  if (!shouldPopulateField(density, index, 'medium')) return {};
  
  return {
    theme_name: randomItem(['Space Adventure', 'Haunted Mansion', 'Underwater Journey', 'Fantasy Quest']),
    story_description: 'An immersive journey through a themed environment with exciting scenes.',
    show_duration_seconds: randomInt(180, 600),
    animatronics_count: randomInt(5, 50),
    projection_type: randomItem(['2d', '3d', 'holographic', 'mixed']),
    ride_system: randomItem(['omnimover', 'tracked', 'trackless', 'boat']),
    scenes_count: randomInt(5, 20)
  };
}

/**
 * Generate flat ride specific fields
 */
export function generateFlatRideFields(density: FieldDensity, index: number) {
  if (!shouldPopulateField(density, index, 'medium')) return {};
  
  return {
    rotation_type: randomItem(['horizontal', 'vertical', 'both', 'none']),
    motion_pattern: randomItem(['circular', 'pendulum', 'spinning', 'wave', 'random']),
    platform_count: randomInt(1, 8),
    swing_angle_degrees: randomInt(45, 180),
    rotation_speed_rpm: randomInt(5, 30),
    arm_length_meters: randomFloat(5, 25, 1),
    max_height_reached_meters: randomFloat(10, 80, 1)
  };
}

/**
 * Generate kiddie ride specific fields
 */
export function generateKiddieRideFields(density: FieldDensity, index: number) {
  if (!shouldPopulateField(density, index, 'medium')) return {};
  
  return {
    min_age: randomInt(2, 5),
    max_age: randomInt(8, 12),
    educational_theme: randomItem(['counting', 'colors', 'animals', 'shapes', 'letters']),
    character_theme: randomItem(['dinosaurs', 'princesses', 'superheroes', 'animals', 'vehicles'])
  };
}

/**
 * Generate transportation ride specific fields
 */
export function generateTransportationRideFields(density: FieldDensity, index: number) {
  if (!shouldPopulateField(density, index, 'medium')) return {};
  
  return {
    transport_type: randomItem(['monorail', 'train', 'skyway', 'gondola', 'ferry']),
    route_length_meters: randomInt(500, 5000),
    stations_count: randomInt(2, 8),
    vehicle_capacity: randomInt(20, 200),
    vehicles_count: randomInt(2, 12),
    round_trip_duration_seconds: randomInt(300, 1800)
  };
}

/**
 * Generate material arrays for rides
 */
export function generateRideMaterials(category: string) {
  if (category === 'roller_coaster') {
    return {
      track_material: [randomItem(['steel', 'wood', 'hybrid'])],
      support_material: [randomItem(['steel', 'wood', 'concrete'])],
      propulsion_method: [randomItem(['chain_lift', 'cable_lift', 'launch', 'gravity', 'tire_drive'])]
    };
  }
  return {};
}

/**
 * Generate source URL for testing
 */
export function generateSourceUrl(entityType: string, index: number): string {
  return `https://example.com/source/${entityType}/${index + 1}`;
}

/**
 * Generate submission notes based on field density
 */
export function generateSubmissionNotes(
  density: FieldDensity,
  index: number,
  entityType: string
): string | undefined {
  if (!shouldPopulateField(density, index, 'low')) return undefined;
  
  const notes = [
    `Updated ${entityType} information from official source`,
    `Added comprehensive data for testing purposes`,
    `Verified information through multiple sources`,
    `Historical data added for completeness`,
    `Technical specifications verified with manufacturer`
  ];
  
  return randomItem(notes);
}
