import 'package:flutter/foundation.dart'; @immutable class SupportChat { final String id; final String userId; final DateTime createdAt; final DateTime? lastMessageAt; final DateTime? lastSeenAt; const SupportChat({ required this.id, required this.userId, required this.createdAt, this.lastMessageAt, this.lastSeenAt, }); factory SupportChat.fromJson(Map json) { return SupportChat( id: json['id'] as String, userId: json['user_id'] as String, createdAt: DateTime.parse(json['created_at'] as String), lastMessageAt: _parseDate(json['last_message_at']), lastSeenAt: _parseDate(json['last_seen_at']), ); } } enum SupportMessageSenderRole { user, admin } @immutable class SupportMessage { final String id; final String chatId; final String senderId; final SupportMessageSenderRole senderRole; final String body; final String? parentMessageId; final DateTime createdAt; final List attachments; const SupportMessage({ required this.id, required this.chatId, required this.senderId, required this.senderRole, required this.body, required this.parentMessageId, required this.createdAt, required this.attachments, }); factory SupportMessage.fromJson(Map json) { final rawAttachments = json['attachments']; final attachments = rawAttachments is List ? rawAttachments .whereType() .map((e) => SupportAttachment.fromJson(Map.from(e))) .toList(growable: false) : const []; return SupportMessage( id: json['id'] as String, chatId: json['chat_id'] as String, senderId: json['sender_id'] as String, senderRole: json['sender_role'] == 'admin' ? SupportMessageSenderRole.admin : SupportMessageSenderRole.user, body: (json['body'] as String?) ?? '', parentMessageId: json['parent_message_id'] as String?, createdAt: DateTime.parse(json['created_at'] as String), attachments: attachments, ); } } @immutable class SupportAttachment { final String id; final String messageId; final String storagePath; final String fileName; final String mimeType; final DateTime createdAt; const SupportAttachment({ required this.id, required this.messageId, required this.storagePath, required this.fileName, required this.mimeType, required this.createdAt, }); factory SupportAttachment.fromJson(Map json) { return SupportAttachment( id: json['id'] as String, messageId: json['message_id'] as String, storagePath: json['storage_path'] as String, fileName: (json['file_name'] as String?) ?? 'attachment', mimeType: (json['mime_type'] as String?) ?? 'application/octet-stream', createdAt: DateTime.parse(json['created_at'] as String), ); } } /// The object returned by the upload-support-attachment endpoint, ready to be /// included in the send-support-message body's `attachment_paths` array. @immutable class SupportAttachmentUpload { final String storagePath; final String fileName; final String mimeType; const SupportAttachmentUpload({ required this.storagePath, required this.fileName, required this.mimeType, }); factory SupportAttachmentUpload.fromJson(Map json) { return SupportAttachmentUpload( storagePath: json['storage_path'] as String, fileName: (json['file_name'] as String?) ?? 'attachment', mimeType: (json['mime_type'] as String?) ?? 'application/octet-stream', ); } Map toJson() => { 'storage_path': storagePath, 'file_name': fileName, 'mime_type': mimeType, }; } @immutable class SupportIssue { final String id; final String title; final String? description; final String? helpBlogSlug; final String? helpVideoUrl; const SupportIssue({ required this.id, required this.title, this.description, this.helpBlogSlug, this.helpVideoUrl, }); factory SupportIssue.fromJson(Map json) { return SupportIssue( id: json['id'] as String, title: (json['title'] as String?) ?? '', description: json['description'] as String?, helpBlogSlug: json['help_blog_slug'] as String?, helpVideoUrl: json['help_video_url'] as String?, ); } } DateTime? _parseDate(dynamic value) { if (value is String && value.isNotEmpty) { return DateTime.tryParse(value); } return null; }