createReport method

Future<String> createReport(
  1. String userId,
  2. String type,
  3. GeoPoint location,
  4. Map<String, dynamic> details, {
  5. int? direction,
  6. int? lane,
  7. String suggestedAlternative = 'none',
})

//////////// //////////// Creates a new report in Firestore.

Implementation

// II.B - Report CRUD Operations
///////////////
/// Creates a new report in Firestore.
Future<String> createReport(
  String userId,
  String type,
  GeoPoint location, // Firestore GeoPoint
  Map<String, dynamic> details, {
  int? direction,
  int? lane,
  String suggestedAlternative = 'none',
}) async {
  try {
    final expiresAt = Timestamp.fromDate(
      DateTime.now().add(Duration(hours: _expirationHours[type] ?? 24))
    );

    // Convert GeoPoint to RaliPosition for consistent handling
    final raliPosition = RaliPosition.fromFirebaseGeoPoint(location);

    // Calculate the geohash using the RaliPosition
    final geoFirePoint = _geo.point(latitude: raliPosition.lat, longitude: raliPosition.lng);

    // Create the data to be stored in Firestore
    final reportData = {
      'type': type,
      'location': location, // Store the Firestore GeoPoint
      'geohash': geoFirePoint.hash,
      'geopoint': geoFirePoint.data, // GeoFlutterFire's formatted data
      'createdBy': userId,
      'createdAt': Timestamp.now(),
      'expiresAt': expiresAt,
      'direction': direction ?? 0,
      'lane': lane ?? 0,
      'suggestedAlternative': suggestedAlternative,
      'details': details,
    };

    final docRef = await _firestore.collection(_collectionPath).add(reportData);
    return docRef.id;
  } catch (e) {
    throw Exception('Error creating report: $e');
  }
}