getMosquitoSpeciesByName method

Future<MosquitoSpecies?> getMosquitoSpeciesByName(
  1. String scientificName,
  2. String languageCode
)

Retrieves a mosquito species by its scientific name for a specific language.

Returns a single mosquito species with localized information and associated diseases, or null if not found.

@param scientificName The scientific name of the mosquito species @param languageCode The language code (e.g., 'en', 'es') @return A Future that completes with a MosquitoSpecies or null

Implementation

Future<MosquitoSpecies?> getMosquitoSpeciesByName(String scientificName, String languageCode) async {
  final db = await database;
  print("[DEBUG] DatabaseService: Received name to query: $scientificName with lang: $languageCode");
  final List<Map<String, dynamic>> maps = await db.rawQuery('''
    SELECT
      s.id, s.name, s.image_url,
      t.common_name, t.description, t.habitat, t.distribution
    FROM mosquito_species s
    LEFT JOIN mosquito_species_translations t ON s.id = t.species_id
    WHERE LOWER(s.name) = LOWER(?) AND t.language_code = ?
  ''', [scientificName, languageCode]);

  print("[DEBUG] DatabaseService: Raw query returned ${maps.length} rows.");
  if (maps.isNotEmpty) {
    print("[DEBUG] DatabaseService: First row data: ${maps.first}");
  }
  if (maps.isEmpty) return null;

  final map = maps.first;
  final diseaseNames = await _getDiseaseNamesForMosquito(db, map['id'], languageCode);

  return MosquitoSpecies(
    id: map['id'],
    name: map['name'],
    commonName: map['common_name'],
    description: map['description'],
    habitat: map['habitat'],
    distribution: map['distribution'],
    imageUrl: map['image_url'],
    diseases: diseaseNames,
  );
}