import '../../nostr/models/nostr_profile.dart'; /// Data model representing a user session. /// /// This model stores user identification and authentication information /// for session management and data isolation. class User { /// Unique identifier for the user. final String id; /// Display name or username for the user. final String username; /// Optional authentication token or session token. final String? token; /// Timestamp when the session was created (milliseconds since epoch). final int createdAt; /// Optional Nostr profile data (if logged in via Nostr). final NostrProfile? nostrProfile; /// Creates a [User] instance. /// /// [id] - Unique identifier for the user. /// [username] - Display name or username. /// [token] - Optional authentication token. /// [createdAt] - Session creation timestamp (defaults to current time). /// [nostrProfile] - Optional Nostr profile data. User({ required this.id, required this.username, this.token, int? createdAt, this.nostrProfile, }) : createdAt = createdAt ?? DateTime.now().millisecondsSinceEpoch; /// Creates a [User] from a Map (e.g., from database or JSON). factory User.fromMap(Map map) { return User( id: map['id'] as String, username: map['username'] as String, token: map['token'] as String?, createdAt: map['created_at'] as int?, nostrProfile: map['nostr_profile'] != null ? NostrProfile.fromJson(map['nostr_profile'] as Map) : null, ); } /// Converts the [User] to a Map for storage. Map toMap() { return { 'id': id, 'username': username, 'token': token, 'created_at': createdAt, 'nostr_profile': nostrProfile?.toJson(), }; } /// Creates a copy of this [User] with updated fields. User copyWith({ String? id, String? username, String? token, int? createdAt, NostrProfile? nostrProfile, }) { return User( id: id ?? this.id, username: username ?? this.username, token: token ?? this.token, createdAt: createdAt ?? this.createdAt, nostrProfile: nostrProfile ?? this.nostrProfile, ); } @override String toString() { return 'User(id: $id, username: $username, createdAt: $createdAt)'; } @override bool operator ==(Object other) { if (identical(this, other)) return true; return other is User && other.id == id; } @override int get hashCode => id.hashCode; }