import 'dart:convert'; /// Model representing a user in the travel mate application. /// /// This class encapsulates user information including personal details /// and provides methods for serialization/deserialization with Firebase /// and JSON operations. class User { /// Unique identifier for the user (usually Firebase UID). final String? id; /// User's last name. final String nom; /// User's first name. final String prenom; /// User's email address. final String email; /// Creates a new [User] instance. /// /// [nom], [prenom], and [email] are required fields. /// [id] is optional and typically assigned by Firebase. User({ this.id, required this.nom, required this.prenom, required this.email, }); /// Creates a [User] instance from a Map (useful for Firebase operations). /// /// Handles null values gracefully by providing empty string defaults. factory User.fromMap(Map map) { return User( id: map['id'], nom: map['nom'] ?? '', prenom: map['prenom'] ?? '', email: map['email'] ?? '', ); } /// Creates a [User] instance from a JSON string. /// /// Parses the JSON and delegates to [fromMap] for object creation. factory User.fromJson(String jsonStr) { Map map = json.decode(jsonStr); return User.fromMap(map); } /// Converts the [User] instance to a Map (useful for Firebase operations). /// /// Returns a map with all user properties for database storage. Map toMap() { return { 'id': id, 'nom': nom, 'prenom': prenom, 'email': email, }; } /// Converts the [User] instance to a JSON string. /// /// Useful for API communications and data serialization. String toJson() { return json.encode(toMap()); } /// Gets the user's full name in "first last" format. String get fullName => '$prenom $nom'; /// Creates a copy of this user with optionally modified properties. /// /// Allows updating specific fields while preserving others. /// Useful for state management and partial updates. User copyWith({ String? id, String? nom, String? prenom, String? email, }) { return User( id: id ?? this.id, nom: nom ?? this.nom, prenom: prenom ?? this.prenom, email: email ?? this.email, ); } /// Returns a string representation of the user. @override String toString() { return 'User(id: $id, nom: $nom, prenom: $prenom, email: $email)'; } /// Compares users based on email address. /// /// Two users are considered equal if they have the same email. @override bool operator ==(Object other) { if (identical(this, other)) return true; return other is User && other.email == email; } /// Hash code based on email address. @override int get hashCode => email.hashCode; }