/// A selectable Indian city shown flight-booking style: "Chennai (MAA)".
class IndiaCity {
  final String name;
  final String code;  // airport-style 3-letter code
  final String state;

  const IndiaCity(this.name, this.code, this.state);

  String get label => '$name ($code)';

  /// True if [query] matches the city name, code, or state.
  bool matches(String query) {
    final q = query.trim().toLowerCase();
    if (q.isEmpty) return true;
    return name.toLowerCase().contains(q) ||
        code.toLowerCase().contains(q) ||
        state.toLowerCase().contains(q);
  }
}

/// Major Indian cities with flight-style codes. Tamil Nadu listed first
/// (primary operating region), then the rest of India.
const List<IndiaCity> indiaCities = [
  // ── Tamil Nadu ──────────────────────────────────────────────────────────
  IndiaCity('Chennai', 'MAA', 'Tamil Nadu'),
  IndiaCity('Coimbatore', 'CJB', 'Tamil Nadu'),
  IndiaCity('Madurai', 'IXM', 'Tamil Nadu'),
  IndiaCity('Tiruchirappalli', 'TRZ', 'Tamil Nadu'),
  IndiaCity('Salem', 'SXV', 'Tamil Nadu'),
  IndiaCity('Tuticorin', 'TCR', 'Tamil Nadu'),
  IndiaCity('Tirupur', 'TUP', 'Tamil Nadu'),
  IndiaCity('Erode', 'ERD', 'Tamil Nadu'),
  IndiaCity('Vellore', 'VLR', 'Tamil Nadu'),
  IndiaCity('Tirunelveli', 'TEN', 'Tamil Nadu'),
  IndiaCity('Thanjavur', 'TNJ', 'Tamil Nadu'),
  IndiaCity('Dindigul', 'DGL', 'Tamil Nadu'),
  IndiaCity('Hosur', 'HSR', 'Tamil Nadu'),
  IndiaCity('Nagercoil', 'NGL', 'Tamil Nadu'),
  IndiaCity('Karur', 'KRR', 'Tamil Nadu'),
  IndiaCity('Namakkal', 'NMK', 'Tamil Nadu'),
  IndiaCity('Kanchipuram', 'KCP', 'Tamil Nadu'),
  IndiaCity('Cuddalore', 'CDL', 'Tamil Nadu'),
  IndiaCity('Thoothukudi', 'TOO', 'Tamil Nadu'),

  // ── Karnataka ───────────────────────────────────────────────────────────
  IndiaCity('Bengaluru', 'BLR', 'Karnataka'),
  IndiaCity('Mysuru', 'MYQ', 'Karnataka'),
  IndiaCity('Mangaluru', 'IXE', 'Karnataka'),
  IndiaCity('Hubli', 'HBX', 'Karnataka'),
  IndiaCity('Belagavi', 'IXG', 'Karnataka'),
  IndiaCity('Kalaburagi', 'GBI', 'Karnataka'),
  IndiaCity('Davangere', 'DVG', 'Karnataka'),
  IndiaCity('Ballari', 'BEP', 'Karnataka'),

  // ── Kerala ──────────────────────────────────────────────────────────────
  IndiaCity('Kochi', 'COK', 'Kerala'),
  IndiaCity('Thiruvananthapuram', 'TRV', 'Kerala'),
  IndiaCity('Kozhikode', 'CCJ', 'Kerala'),
  IndiaCity('Kannur', 'CNN', 'Kerala'),
  IndiaCity('Thrissur', 'TCR2', 'Kerala'),
  IndiaCity('Kollam', 'QLN', 'Kerala'),

  // ── Telangana & Andhra Pradesh ──────────────────────────────────────────
  IndiaCity('Hyderabad', 'HYD', 'Telangana'),
  IndiaCity('Warangal', 'WGC', 'Telangana'),
  IndiaCity('Visakhapatnam', 'VTZ', 'Andhra Pradesh'),
  IndiaCity('Vijayawada', 'VGA', 'Andhra Pradesh'),
  IndiaCity('Tirupati', 'TIR', 'Andhra Pradesh'),
  IndiaCity('Guntur', 'GNT', 'Andhra Pradesh'),
  IndiaCity('Rajahmundry', 'RJA', 'Andhra Pradesh'),
  IndiaCity('Nellore', 'NLR', 'Andhra Pradesh'),

  // ── Maharashtra ─────────────────────────────────────────────────────────
  IndiaCity('Mumbai', 'BOM', 'Maharashtra'),
  IndiaCity('Pune', 'PNQ', 'Maharashtra'),
  IndiaCity('Nagpur', 'NAG', 'Maharashtra'),
  IndiaCity('Nashik', 'ISK', 'Maharashtra'),
  IndiaCity('Aurangabad', 'IXU', 'Maharashtra'),
  IndiaCity('Kolhapur', 'KLH', 'Maharashtra'),
  IndiaCity('Solapur', 'SSE', 'Maharashtra'),

  // ── Delhi NCR & North ───────────────────────────────────────────────────
  IndiaCity('New Delhi', 'DEL', 'Delhi'),
  IndiaCity('Gurugram', 'GGN', 'Haryana'),
  IndiaCity('Noida', 'NOI', 'Uttar Pradesh'),
  IndiaCity('Faridabad', 'FBD', 'Haryana'),
  IndiaCity('Chandigarh', 'IXC', 'Chandigarh'),
  IndiaCity('Amritsar', 'ATQ', 'Punjab'),
  IndiaCity('Ludhiana', 'LUH', 'Punjab'),
  IndiaCity('Jalandhar', 'JLR2', 'Punjab'),

  // ── Gujarat ─────────────────────────────────────────────────────────────
  IndiaCity('Ahmedabad', 'AMD', 'Gujarat'),
  IndiaCity('Surat', 'STV', 'Gujarat'),
  IndiaCity('Vadodara', 'BDQ', 'Gujarat'),
  IndiaCity('Rajkot', 'RAJ', 'Gujarat'),
  IndiaCity('Bhavnagar', 'BHU', 'Gujarat'),
  IndiaCity('Jamnagar', 'JGA', 'Gujarat'),

  // ── Rajasthan ───────────────────────────────────────────────────────────
  IndiaCity('Jaipur', 'JAI', 'Rajasthan'),
  IndiaCity('Jodhpur', 'JDH', 'Rajasthan'),
  IndiaCity('Udaipur', 'UDR', 'Rajasthan'),
  IndiaCity('Kota', 'KTU', 'Rajasthan'),
  IndiaCity('Bikaner', 'BKB', 'Rajasthan'),

  // ── Uttar Pradesh & Central ─────────────────────────────────────────────
  IndiaCity('Lucknow', 'LKO', 'Uttar Pradesh'),
  IndiaCity('Kanpur', 'KNU', 'Uttar Pradesh'),
  IndiaCity('Varanasi', 'VNS', 'Uttar Pradesh'),
  IndiaCity('Agra', 'AGR', 'Uttar Pradesh'),
  IndiaCity('Prayagraj', 'IXD', 'Uttar Pradesh'),
  IndiaCity('Gorakhpur', 'GOP', 'Uttar Pradesh'),
  IndiaCity('Bhopal', 'BHO', 'Madhya Pradesh'),
  IndiaCity('Indore', 'IDR', 'Madhya Pradesh'),
  IndiaCity('Gwalior', 'GWL', 'Madhya Pradesh'),
  IndiaCity('Jabalpur', 'JLR', 'Madhya Pradesh'),
  IndiaCity('Raipur', 'RPR', 'Chhattisgarh'),

  // ── East & North-East ───────────────────────────────────────────────────
  IndiaCity('Kolkata', 'CCU', 'West Bengal'),
  IndiaCity('Siliguri', 'IXB', 'West Bengal'),
  IndiaCity('Durgapur', 'RDP', 'West Bengal'),
  IndiaCity('Patna', 'PAT', 'Bihar'),
  IndiaCity('Gaya', 'GAY', 'Bihar'),
  IndiaCity('Ranchi', 'IXR', 'Jharkhand'),
  IndiaCity('Jamshedpur', 'JSD', 'Jharkhand'),
  IndiaCity('Bhubaneswar', 'BBI', 'Odisha'),
  IndiaCity('Cuttack', 'CTC', 'Odisha'),
  IndiaCity('Guwahati', 'GAU', 'Assam'),
  IndiaCity('Dibrugarh', 'DIB', 'Assam'),
  IndiaCity('Agartala', 'IXA', 'Tripura'),
  IndiaCity('Imphal', 'IMF', 'Manipur'),
  IndiaCity('Shillong', 'SHL', 'Meghalaya'),
  IndiaCity('Aizawl', 'AJL', 'Mizoram'),
  IndiaCity('Dimapur', 'DMU', 'Nagaland'),

  // ── Hills, UTs & others ─────────────────────────────────────────────────
  IndiaCity('Dehradun', 'DED', 'Uttarakhand'),
  IndiaCity('Haridwar', 'HDW', 'Uttarakhand'),
  IndiaCity('Shimla', 'SLV', 'Himachal Pradesh'),
  IndiaCity('Srinagar', 'SXR', 'Jammu & Kashmir'),
  IndiaCity('Jammu', 'IXJ', 'Jammu & Kashmir'),
  IndiaCity('Leh', 'IXL', 'Ladakh'),
  IndiaCity('Panaji', 'GOI', 'Goa'),
  IndiaCity('Puducherry', 'PNY', 'Puducherry'),
  IndiaCity('Port Blair', 'IXZ', 'Andaman & Nicobar Islands'),
];

/// Finds a city by (loose) name match — used to map a GPS reverse-geocode
/// result to a known city. Returns null if nothing reasonable matches.
IndiaCity? findCityByName(String? name) {
  if (name == null || name.trim().isEmpty) return null;
  final n = name.trim().toLowerCase();
  for (final c in indiaCities) {
    if (c.name.toLowerCase() == n) return c;
  }
  // Loose contains match (e.g. "Coimbatore District" → "Coimbatore")
  for (final c in indiaCities) {
    if (n.contains(c.name.toLowerCase()) ||
        c.name.toLowerCase().contains(n)) {
      return c;
    }
  }
  return null;
}
