import 'dart:convert'; /// Represents an item in the shared packing list for a trip. /// /// Each item stores a [label], completion flag [isPacked], and an optional /// [assignee] to indicate who prend en charge l'item. class PackingItem { /// Unique identifier for the packing entry. final String id; /// Text displayed in the list (ex: "Adaptateur US", "Pharmacie"). final String label; /// Whether the item is already packed. final bool isPacked; /// Optional assignee (user id or name) for accountability. final String? assignee; /// Creation timestamp for ordering. final DateTime createdAt; /// Creates a packing item. const PackingItem({ required this.id, required this.label, required this.isPacked, required this.createdAt, this.assignee, }); /// Factory to create a new item in not-packed state. factory PackingItem.newItem({ required String id, required String label, String? assignee, }) { return PackingItem( id: id, label: label, assignee: assignee, isPacked: false, createdAt: DateTime.now().toUtc(), ); } /// Returns a copy with modifications. PackingItem copyWith({ String? id, String? label, bool? isPacked, String? assignee, DateTime? createdAt, }) { return PackingItem( id: id ?? this.id, label: label ?? this.label, isPacked: isPacked ?? this.isPacked, assignee: assignee ?? this.assignee, createdAt: createdAt ?? this.createdAt, ); } /// JSON serialization helper. Map toJson() { return { 'id': id, 'label': label, 'isPacked': isPacked, 'assignee': assignee, 'createdAt': createdAt.toIso8601String(), }; } /// JSON deserialization helper. factory PackingItem.fromJson(Map json) { return PackingItem( id: json['id'] as String, label: json['label'] as String, isPacked: json['isPacked'] as bool? ?? false, assignee: json['assignee'] as String?, createdAt: DateTime.parse(json['createdAt'] as String), ); } /// Encodes list to JSON string. static String encodeList(List items) { return json.encode(items.map((i) => i.toJson()).toList()); } /// Decodes list from JSON string. static List decodeList(String raw) { final decoded = json.decode(raw) as List; return decoded .cast>() .map(PackingItem.fromJson) .toList(); } }