import 'dart:convert'; /// Represents an emergency contact for a trip (person or service). /// /// Stores basic contact details and optional notes for quick access during /// critical situations. class EmergencyContact { /// Unique identifier for the contact entry. final String id; /// Display name (ex: "Ambassade", "Marie", "Assurance Europ"). final String name; /// Phone number in international format when possible. final String phone; /// Optional description or role (ex: "Assistance médicale", "Famille"). final String? note; /// Creation timestamp for stable ordering. final DateTime createdAt; /// Creates an emergency contact entry. const EmergencyContact({ required this.id, required this.name, required this.phone, required this.createdAt, this.note, }); /// Builds a new contact with current timestamp. factory EmergencyContact.newContact({ required String id, required String name, required String phone, String? note, }) { return EmergencyContact( id: id, name: name, phone: phone, note: note, createdAt: DateTime.now().toUtc(), ); } /// Returns a copy with updated fields. EmergencyContact copyWith({ String? id, String? name, String? phone, String? note, DateTime? createdAt, }) { return EmergencyContact( id: id ?? this.id, name: name ?? this.name, phone: phone ?? this.phone, note: note ?? this.note, createdAt: createdAt ?? this.createdAt, ); } /// Serializes contact to JSON. Map toJson() { return { 'id': id, 'name': name, 'phone': phone, 'note': note, 'createdAt': createdAt.toIso8601String(), }; } /// Deserializes contact from JSON. factory EmergencyContact.fromJson(Map json) { return EmergencyContact( id: json['id'] as String, name: json['name'] as String, phone: json['phone'] as String, note: json['note'] as String?, createdAt: DateTime.parse(json['createdAt'] as String), ); } /// Encodes list to JSON string. static String encodeList(List contacts) { return json.encode(contacts.map((c) => c.toJson()).toList()); } /// Decodes list from JSON string. static List decodeList(String raw) { final decoded = json.decode(raw) as List; return decoded .cast>() .map(EmergencyContact.fromJson) .toList(); } }