/**
 * Theme Park Terminology Glossary
 * Comprehensive definitions for technical terms used in forms
 */

export interface GlossaryTerm {
  term: string;
  category: 'manufacturer' | 'technology' | 'element' | 'component' | 'measurement' | 'type' | 'material';
  definition: string;
  example?: string;
  relatedTerms?: string[];
}

export const glossary: Record<string, GlossaryTerm> = {
  // Manufacturers
  'rmc': {
    term: 'RMC',
    category: 'manufacturer',
    definition: 'Rocky Mountain Construction - Manufacturer known for hybrid coasters with steel IBox track on wooden structures',
    example: 'Steel Vengeance at Cedar Point',
    relatedTerms: ['ibox-track', 'hybrid-coaster'],
  },
  'intamin': {
    term: 'Intamin',
    category: 'manufacturer',
    definition: 'Swiss manufacturer known for record-breaking coasters and innovative launch systems',
    example: 'Millennium Force, Top Thrill Dragster',
    relatedTerms: ['hydraulic-launch', 'lsm'],
  },
  'b&m': {
    term: 'B&M',
    category: 'manufacturer',
    definition: 'Bolliger & Mabillard - Swiss manufacturer known for smooth, reliable coasters',
    example: 'Fury 325, Banshee, GateKeeper',
    relatedTerms: ['inverted', 'wing-coaster', 'dive-coaster'],
  },
  'vekoma': {
    term: 'Vekoma',
    category: 'manufacturer',
    definition: 'Dutch manufacturer with wide range from family coasters to intense thrill rides',
    example: 'Space Mountain (Disney), Thunderbird (PowerPark)',
  },
  'gerstlauer': {
    term: 'Gerstlauer',
    category: 'manufacturer',
    definition: 'German manufacturer known for compact, intense coasters with vertical lifts',
    example: 'Takabisha (steepest drop), Karacho',
    relatedTerms: ['euro-fighter'],
  },
  's&s': {
    term: 'S&S',
    category: 'manufacturer',
    definition: 'S&S Worldwide - American manufacturer of compressed-air launch coasters and thrill rides',
    example: 'Hypersonic XLC, Screamin\' Swing',
    relatedTerms: ['compressed-air-launch'],
  },
  
  // Launch/Propulsion Systems
  'lsm': {
    term: 'LSM Launch',
    category: 'technology',
    definition: 'Linear Synchronous Motor - Uses electromagnetic propulsion to smoothly accelerate trains',
    example: 'Maverick, Taron, Velocicoaster',
    relatedTerms: ['lim', 'magnetic-launch'],
  },
  'lim': {
    term: 'LIM Launch',
    category: 'technology',
    definition: 'Linear Induction Motor - Earlier electromagnetic launch technology, less efficient than LSM',
    example: 'Flight of Fear, Rock \'n\' Roller Coaster',
    relatedTerms: ['lsm'],
  },
  'hydraulic-launch': {
    term: 'Hydraulic Launch',
    category: 'technology',
    definition: 'Uses hydraulic winch system to rapidly accelerate train, capable of extreme speeds',
    example: 'Top Thrill Dragster, Kingda Ka (fastest launches)',
    relatedTerms: ['intamin'],
  },
  'chain-lift': {
    term: 'Chain Lift',
    category: 'technology',
    definition: 'Traditional lift system using chain and anti-rollback dogs',
    example: 'Most traditional wooden and steel coasters',
  },
  'cable-lift': {
    term: 'Cable Lift',
    category: 'technology',
    definition: 'Uses steel cable for faster lift speeds than chain',
    example: 'Millennium Force (first major use)',
  },
  'compressed-air-launch': {
    term: 'Compressed Air Launch',
    category: 'technology',
    definition: 'Uses compressed air to launch train, very powerful acceleration',
    example: 'Hypersonic XLC, Do-Dodonpa',
    relatedTerms: ['s&s'],
  },
  
  // Coaster Types
  'inverted': {
    term: 'Inverted Coaster',
    category: 'type',
    definition: 'Train runs below the track with feet dangling, track above riders',
    example: 'Banshee, Montu, Raptor',
    relatedTerms: ['b&m'],
  },
  'wing-coaster': {
    term: 'Wing Coaster',
    category: 'type',
    definition: 'Seats extend to sides of track with nothing above or below riders',
    example: 'GateKeeper, The Swarm, X-Flight',
    relatedTerms: ['b&m'],
  },
  'dive-coaster': {
    term: 'Dive Coaster',
    category: 'type',
    definition: 'Features wide trains and vertical/near-vertical first drop, often with holding brake',
    example: 'Valravn, SheiKra, Griffon',
    relatedTerms: ['b&m'],
  },
  'flying-coaster': {
    term: 'Flying Coaster',
    category: 'type',
    definition: 'Riders positioned face-down in flying position',
    example: 'Tatsu, Manta, Flying Dinosaur',
    relatedTerms: ['b&m', 'vekoma'],
  },
  'hyper-coaster': {
    term: 'Hyper Coaster',
    category: 'type',
    definition: 'Coaster between 200-299 feet tall, focused on airtime',
    example: 'Diamondback, Nitro, Apollo\'s Chariot',
    relatedTerms: ['giga-coaster', 'airtime'],
  },
  'giga-coaster': {
    term: 'Giga Coaster',
    category: 'type',
    definition: 'Coaster between 300-399 feet tall',
    example: 'Millennium Force, Fury 325, Leviathan',
    relatedTerms: ['hyper-coaster', 'strata-coaster'],
  },
  'strata-coaster': {
    term: 'Strata Coaster',
    category: 'type',
    definition: 'Coaster 400+ feet tall',
    example: 'Top Thrill Dragster, Kingda Ka',
    relatedTerms: ['giga-coaster'],
  },
  'hybrid-coaster': {
    term: 'Hybrid Coaster',
    category: 'type',
    definition: 'Steel track on wooden support structure',
    example: 'Steel Vengeance, Twisted Colossus',
    relatedTerms: ['rmc', 'ibox-track'],
  },
  'euro-fighter': {
    term: 'Euro-Fighter',
    category: 'type',
    definition: 'Compact Gerstlauer coaster with vertical lift and beyond-vertical drop',
    example: 'Takabisha, Saw: The Ride',
    relatedTerms: ['gerstlauer'],
  },
  
  // Track Materials
  'ibox-track': {
    term: 'IBox Track',
    category: 'material',
    definition: 'RMC\'s steel box-beam track system used on hybrid coasters, allows extreme elements',
    example: 'Steel Vengeance, Iron Rattler',
    relatedTerms: ['rmc', 'hybrid-coaster', 'topper-track'],
  },
  'topper-track': {
    term: 'Topper Track',
    category: 'material',
    definition: 'RMC\'s steel plate topper on wooden track for smoother wooden coaster experience',
    example: 'Outlaw Run, Lightning Rod',
    relatedTerms: ['rmc', 'ibox-track'],
  },
  
  // Restraint Types
  'otsr': {
    term: 'OTSR',
    category: 'component',
    definition: 'Over-The-Shoulder Restraint - Safety harness that goes over shoulders and locks at waist',
    example: 'Used on most inverting coasters',
    relatedTerms: ['vest-restraint', 'lap-bar'],
  },
  'lap-bar': {
    term: 'Lap Bar',
    category: 'component',
    definition: 'Restraint that only crosses the lap/thighs, offers more freedom',
    example: 'Millennium Force, most airtime-focused rides',
    relatedTerms: ['otsr', 't-bar'],
  },
  't-bar': {
    term: 'T-Bar',
    category: 'component',
    definition: 'T-shaped lap bar restraint, common on Intamin hyper coasters',
    example: 'Intimidator 305, Skyrush',
    relatedTerms: ['lap-bar'],
  },
  'vest-restraint': {
    term: 'Vest Restraint',
    category: 'component',
    definition: 'Soft vest-style over-shoulder restraint, more comfortable than traditional OTSR',
    example: 'GateKeeper, Valravn (B&M)',
    relatedTerms: ['otsr'],
  },
  'shin-bar': {
    term: 'Shin Bar',
    category: 'component',
    definition: 'Additional restraint that holds shins in place, used on some intense rides',
    example: 'Flying coasters, some Vekoma rides',
  },
  
  // Elements
  'airtime': {
    term: 'Airtime',
    category: 'element',
    definition: 'Negative G-forces that create sensation of floating or being lifted from seat',
    example: 'Camelback hills, speed hills',
    relatedTerms: ['ejector-airtime', 'floater-airtime', 'hangtime'],
  },
  'ejector-airtime': {
    term: 'Ejector Airtime',
    category: 'element',
    definition: 'Strong negative Gs that forcefully lift riders from seats',
    example: 'El Toro, Skyrush airtime hills',
    relatedTerms: ['airtime'],
  },
  'floater-airtime': {
    term: 'Floater Airtime',
    category: 'element',
    definition: 'Gentle negative Gs that create sustained floating sensation',
    example: 'B&M hyper coasters',
    relatedTerms: ['airtime'],
  },
  'hangtime': {
    term: 'Hangtime',
    category: 'element',
    definition: 'Suspension in mid-air during inversion, typically at apex of element',
    example: 'Zero-g rolls, inversions on dive coasters',
    relatedTerms: ['airtime', 'inversion'],
  },
  'inversion': {
    term: 'Inversion',
    category: 'element',
    definition: 'Element where riders are turned upside down (≥90 degrees from upright)',
    example: 'Loops, corkscrews, barrel rolls',
    relatedTerms: ['zero-g-roll', 'corkscrew', 'loop'],
  },
  'zero-g-roll': {
    term: 'Zero-G Roll',
    category: 'element',
    definition: 'Heartline inversion with sustained weightlessness',
    example: 'Common on Intamin and B&M coasters',
    relatedTerms: ['inversion', 'hangtime'],
  },
  'corkscrew': {
    term: 'Corkscrew',
    category: 'element',
    definition: 'Inversion where track twists 360 degrees while moving forward',
    example: 'Classic Arrow element',
    relatedTerms: ['inversion'],
  },
  'loop': {
    term: 'Vertical Loop',
    category: 'element',
    definition: 'Full 360-degree vertical circle',
    example: 'Classic clothoid loop shape',
    relatedTerms: ['inversion'],
  },
  'dive-loop': {
    term: 'Dive Loop',
    category: 'element',
    definition: 'Half loop up, half corkscrew down',
    example: 'Common on B&M coasters',
    relatedTerms: ['immelmann', 'inversion'],
  },
  'immelmann': {
    term: 'Immelmann',
    category: 'element',
    definition: 'Half loop up, half roll out (opposite of dive loop)',
    example: 'Named after WWI pilot maneuver',
    relatedTerms: ['dive-loop', 'inversion'],
  },
  'cobra-roll': {
    term: 'Cobra Roll',
    category: 'element',
    definition: 'Double inversion creating S-shape, reversing direction',
    example: 'Common on Vekoma and B&M loopers',
    relatedTerms: ['inversion'],
  },
  'heartline-roll': {
    term: 'Heartline Roll',
    category: 'element',
    definition: 'Barrel roll rotating around rider\'s heartline for smooth inversion',
    example: 'Maverick, many Intamin coasters',
    relatedTerms: ['zero-g-roll', 'inversion'],
  },
  
  // Technical Terms
  'mcbr': {
    term: 'MCBR',
    category: 'component',
    definition: 'Mid-Course Brake Run - Safety brake zone that divides track into blocks',
    example: 'Allows multiple trains to operate safely',
    relatedTerms: ['block-section'],
  },
  'block-section': {
    term: 'Block Section',
    category: 'component',
    definition: 'Track section that only one train can occupy at a time for safety',
    example: 'Station, lift hill, brake runs',
    relatedTerms: ['mcbr'],
  },
  'trim-brake': {
    term: 'Trim Brake',
    category: 'component',
    definition: 'Brake that slows train slightly to control speed',
    example: 'Often on hills or before elements',
  },
  'transfer-track': {
    term: 'Transfer Track',
    category: 'component',
    definition: 'Movable track section for adding/removing trains from circuit',
    example: 'Allows storage of extra trains',
  },
  'anti-rollback': {
    term: 'Anti-Rollback',
    category: 'component',
    definition: 'Safety device preventing train from rolling backward on lift',
    example: 'Creates "clicking" sound on chain lifts',
  },
  
  // Measurements
  'g-force': {
    term: 'G-Force',
    category: 'measurement',
    definition: 'Force of gravity felt by riders. 1G = normal gravity, positive = pushed into seat, negative = lifted from seat',
    example: '4.5G on intense loops, -1.5G on airtime hills',
  },
  'kilometers-per-hour': {
    term: 'km/h',
    category: 'measurement',
    definition: 'Speed measurement in kilometers per hour (metric)',
    example: '193 km/h = 120 mph',
  },
  'meters': {
    term: 'Meters',
    category: 'measurement',
    definition: 'Length/height measurement (metric). 1 meter ≈ 3.28 feet',
    example: '94 meters = 310 feet',
  },
};

/**
 * Get glossary term by key (normalized)
 */
export function getGlossaryTerm(term: string): GlossaryTerm | undefined {
  const key = term.toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
  return glossary[key];
}

/**
 * Search glossary by query
 */
export function searchGlossary(query: string): GlossaryTerm[] {
  const lowerQuery = query.toLowerCase();
  return Object.values(glossary).filter(term =>
    term.term.toLowerCase().includes(lowerQuery) ||
    term.definition.toLowerCase().includes(lowerQuery) ||
    term.example?.toLowerCase().includes(lowerQuery)
  );
}

/**
 * Get all terms in a category
 */
export function getTermsByCategory(category: GlossaryTerm['category']): GlossaryTerm[] {
  return Object.values(glossary).filter(term => term.category === category);
}

/**
 * Get all categories
 */
export function getAllCategories(): GlossaryTerm['category'][] {
  return ['manufacturer', 'technology', 'element', 'component', 'measurement', 'type', 'material'];
}
