class SupportConversationModel { const SupportConversationModel({ required this.id, required this.status, this.unreadUser = 0, }); final String id; final String status; final int unreadUser; factory SupportConversationModel.fromJson(Map json) { return SupportConversationModel( id: json['id'] as String, status: (json['status'] ?? 'open').toString(), unreadUser: (json['unreadUser'] as num?)?.toInt() ?? 0, ); } } class SupportActionPolicy { const SupportActionPolicy({ this.editWindowMinutes = 15, this.deleteWindowMinutes = 15, }); final int editWindowMinutes; final int deleteWindowMinutes; factory SupportActionPolicy.fromJson(Map? json) { if (json == null) return const SupportActionPolicy(); return SupportActionPolicy( editWindowMinutes: (json['editWindowMinutes'] as num?)?.toInt() ?? 15, deleteWindowMinutes: (json['deleteWindowMinutes'] as num?)?.toInt() ?? 15, ); } } class SupportMessageModel { const SupportMessageModel({ required this.id, required this.conversationId, required this.senderType, required this.createdAt, this.senderDisplayName, this.body, this.attachmentUrl, this.attachmentName, this.attachmentMime, this.readAt, this.editedAt, this.deletedAt, this.canEdit = false, this.canDelete = false, this.editAvailableUntil, this.deleteAvailableUntil, this.isPending = false, }); /// Optimistic local bubble waiting for network flush. factory SupportMessageModel.pendingLocal({ required String localId, required String body, String conversationId = '', }) { return SupportMessageModel( id: localId, conversationId: conversationId, senderType: 'user', createdAt: DateTime.now().toUtc().toIso8601String(), body: body, isPending: true, ); } /// Optimistic local image bubble; [filePath] is a device path until flush. factory SupportMessageModel.pendingLocalImage({ required String localId, required String filePath, String conversationId = '', }) { final name = filePath.replaceAll('\\', '/').split('/').last; final ext = name.contains('.') ? name.split('.').last.toLowerCase() : 'jpg'; final mime = switch (ext) { 'png' => 'image/png', 'gif' => 'image/gif', 'webp' => 'image/webp', _ => 'image/jpeg', }; return SupportMessageModel( id: localId, conversationId: conversationId, senderType: 'user', createdAt: DateTime.now().toUtc().toIso8601String(), attachmentUrl: filePath, attachmentName: name, attachmentMime: mime, isPending: true, ); } final String id; final String conversationId; final String senderType; final String createdAt; final String? senderDisplayName; final String? body; final String? attachmentUrl; final String? attachmentName; final String? attachmentMime; final String? readAt; final String? editedAt; final String? deletedAt; final bool canEdit; final bool canDelete; final String? editAvailableUntil; final String? deleteAvailableUntil; /// True for client-queued messages not yet acknowledged by the API. final bool isPending; bool get isDeleted { if (deletedAt != null && deletedAt!.isNotEmpty) return true; final emptyBody = body == null || body!.trim().isEmpty; return emptyBody && (attachmentUrl == null || attachmentUrl!.isEmpty); } bool get isMine => senderType == 'user' || senderType == 'visitor'; bool get isImage { if (attachmentMime?.startsWith('image/') == true) return true; final name = (attachmentName ?? attachmentUrl ?? '').toLowerCase(); return name.endsWith('.jpg') || name.endsWith('.jpeg') || name.endsWith('.png') || name.endsWith('.gif') || name.endsWith('.webp') || name.endsWith('.heic') || name.endsWith('.bmp'); } bool editOpen([SupportActionPolicy? policy]) { return _actionOpen( allowed: canEdit, until: editAvailableUntil, windowMinutes: policy?.editWindowMinutes, ); } bool deleteOpen([SupportActionPolicy? policy]) { return _actionOpen( allowed: canDelete, until: deleteAvailableUntil, windowMinutes: policy?.deleteWindowMinutes, ); } bool _actionOpen({ required bool allowed, required String? until, int? windowMinutes, }) { if (isDeleted || !isMine) return false; if (until != null && until.isNotEmpty) { final dt = DateTime.tryParse(until); if (dt != null) return dt.toUtc().isAfter(DateTime.now().toUtc()); } if (allowed) return true; if (windowMinutes != null && windowMinutes > 0) { final created = DateTime.tryParse(createdAt); if (created == null) return false; return created.toUtc().add(Duration(minutes: windowMinutes)).isAfter(DateTime.now().toUtc()); } return false; } SupportMessageModel copyWith({ String? body, String? readAt, String? editedAt, String? deletedAt, bool? canEdit, bool? canDelete, String? editAvailableUntil, String? deleteAvailableUntil, bool? isPending, bool clearAttachment = false, }) { return SupportMessageModel( id: id, conversationId: conversationId, senderType: senderType, createdAt: createdAt, senderDisplayName: senderDisplayName, body: body ?? this.body, attachmentUrl: clearAttachment ? null : attachmentUrl, attachmentName: clearAttachment ? null : attachmentName, attachmentMime: clearAttachment ? null : attachmentMime, readAt: readAt ?? this.readAt, editedAt: editedAt ?? this.editedAt, deletedAt: deletedAt ?? this.deletedAt, canEdit: canEdit ?? this.canEdit, canDelete: canDelete ?? this.canDelete, editAvailableUntil: editAvailableUntil ?? this.editAvailableUntil, deleteAvailableUntil: deleteAvailableUntil ?? this.deleteAvailableUntil, isPending: isPending ?? this.isPending, ); } factory SupportMessageModel.fromJson(Map json) { return SupportMessageModel( id: json['id'] as String, conversationId: (json['conversationId'] ?? json['conversation_id'] ?? '').toString(), senderType: (json['senderType'] ?? json['sender_type'] ?? 'staff').toString(), createdAt: _readDate(json, 'createdAt', 'created_at'), senderDisplayName: _readString(json, 'senderDisplayName', 'sender_display_name'), body: _readString(json, 'body'), attachmentUrl: _readString(json, 'attachmentUrl', 'attachment_url'), attachmentName: _readString(json, 'attachmentName', 'attachment_name'), attachmentMime: _readString(json, 'attachmentMime', 'attachment_mime'), readAt: _readDate(json, 'readAt', 'read_at'), editedAt: _readDate(json, 'editedAt', 'edited_at'), deletedAt: _readDeletedAt(json), canEdit: _readBool(json, 'canEdit'), canDelete: _readBool(json, 'canDelete'), editAvailableUntil: _readDate(json, 'editAvailableUntil'), deleteAvailableUntil: _readDate(json, 'deleteAvailableUntil'), ); } static String _readDate(Map json, String camel, [String? snake]) { final value = json[camel] ?? (snake == null ? null : json[snake]); if (value == null) return ''; return value.toString(); } static String? _readString(Map json, String camel, [String? snake]) { final value = json[camel] ?? (snake == null ? null : json[snake]); if (value == null) return null; final text = value.toString(); return text.isEmpty ? null : text; } static String? _readDeletedAt(Map json) { if (json['deleted'] == true || json['deleted'] == 1 || json['deleted'] == 'true') { return _readDate(json, 'deletedAt', 'deleted_at').isEmpty ? DateTime.now().toUtc().toIso8601String() : _readDate(json, 'deletedAt', 'deleted_at'); } final value = _readDate(json, 'deletedAt', 'deleted_at'); return value.isEmpty ? null : value; } static bool _readBool(Map json, String key) { final value = json[key]; return value == true || value == 1 || value == 'true'; } } class SupportMessagesPage { const SupportMessagesPage({ required this.messages, required this.staffTyping, this.policy = const SupportActionPolicy(), }); final List messages; final bool staffTyping; final SupportActionPolicy policy; }