// ------------------------------------------------------------
//                     CHAT MODEL
// ------------------------------------------------------------
class ChatModel {
  final bool status;
  final List<MessageModel> messages;

  final String? chatStatus;
  final int? draw;
  final int? recordsTotal;
  final int? recordsFiltered;

  final String? profile;
  final String? name;
  final String? role;

  final List<DocumentModel> userUploadedDocuments;
  final List<DocumentModel> adminUploadedDocuments;

  ChatModel({
    required this.status,
    required this.messages,
    this.chatStatus,
    this.draw,
    this.recordsTotal,
    this.recordsFiltered,
    this.profile,
    this.name,
    this.role,
    required this.userUploadedDocuments,
    required this.adminUploadedDocuments,
  });

  factory ChatModel.fromJson(Map<String, dynamic> json) {
    final msg = json["messages"];

    return ChatModel(
      status: json['status'] ?? false,

      messages: (msg?['data'] != null && msg['data'] is List)
          ? List<MessageModel>.from(
              msg['data'].map((x) => MessageModel.fromJson(x)),
            )
          : [],

      chatStatus: msg?['status'],
      draw: msg?['draw'],
      recordsTotal: msg?['recordsTotal'],
      recordsFiltered: msg?['recordsFiltered'],

      profile: msg?['profile'],
      name: msg?['name'],
      role: msg?['role'],

      userUploadedDocuments:
          (msg?['user_uploaded_documents'] != null &&
              msg['user_uploaded_documents'] is List)
          ? List<DocumentModel>.from(
              msg['user_uploaded_documents'].map(
                (x) => DocumentModel.fromJson(x),
              ),
            )
          : [],

      adminUploadedDocuments:
          (msg?['admin_uploaded_documents'] != null &&
              msg['admin_uploaded_documents'] is List)
          ? List<DocumentModel>.from(
              msg['admin_uploaded_documents'].map(
                (x) => DocumentModel.fromJson(x),
              ),
            )
          : [],
    );
  }
}

// ------------------------------------------------------------
//                     MESSAGE MODEL
// ------------------------------------------------------------
class MessageModel {
  final int id;
  final String chatBy;
  final String message;
  final String type;
  final int isDelivered;
  final int isRead;

  final String? createdAt;
  final String? readAt;
  final String? time;

  final int userId;
  final int? tagId;
  final String? fileName;

  final List<DocumentModel> uploadedDocuments;
  final UserModel? user;
  final ParentTagModel? parentTag;
  final List<DocumentModel> documents;

  MessageModel({
    required this.id,
    required this.chatBy,
    required this.message,
    required this.type,
    required this.isDelivered,
    required this.isRead,
    this.createdAt,
    this.readAt,
    this.time,
    required this.userId,
    this.tagId,
    this.fileName,
    required this.uploadedDocuments,
    this.user,
    this.parentTag,
    required this.documents,
  });

  factory MessageModel.fromJson(Map<String, dynamic> json) {
    return MessageModel(
      id: json['id'] ?? 0,
      chatBy: json['chat_by'] ?? "",
      message: json['message'] ?? "",
      type: json['type'] ?? "",
      isDelivered: json['is_delivered'] ?? 0,
      isRead: json['is_read'] ?? 0,

      createdAt: json['created_at'],
      readAt: json['read_at'],
      time: json['time'],

      userId: json['user_id'] ?? 0,
      tagId: json['tag_id'],
      fileName: json['file_name'],

      uploadedDocuments:
          (json['uploaded_documents'] != null &&
              json['uploaded_documents'] is List)
          ? List<DocumentModel>.from(
              json['uploaded_documents'].map((x) => DocumentModel.fromJson(x)),
            )
          : [],

      documents: (json['documents'] != null && json['documents'] is List)
          ? List<DocumentModel>.from(
              json['documents'].map((x) => DocumentModel.fromJson(x)),
            )
          : [],

      user: json['user'] != null ? UserModel.fromJson(json['user']) : null,
      parentTag: json['parent_tag'] != null
          ? ParentTagModel.fromJson(json['parent_tag'])
          : null,
    );
  }
}

// ------------------------------------------------------------
//                     DOCUMENT MODEL
// ------------------------------------------------------------
class DocumentModel {
  final String filePath;
  final String fileName;
  final String fileType;
  final int? id;
  final String? type;

  DocumentModel({
    this.id,
    required this.filePath,
    required this.fileName,
    required this.fileType,
    this.type,
  });

  factory DocumentModel.fromJson(Map<String, dynamic> json) {
    return DocumentModel(
      id: json['id'],
      filePath: json['file_path'] ?? "",
      fileName: json['file_name'] ?? "",
      fileType: json['file_type'] ?? "",
      type: json['type'],
    );
  }
}

// ------------------------------------------------------------
//                     USER MODEL
// ------------------------------------------------------------
class UserModel {
  final int id;
  final String name;
  final String role;
  final String? profilePicture;
  final Map<String, dynamic>? profile;

  UserModel({
    required this.id,
    required this.name,
    required this.role,
    this.profilePicture,
    this.profile,
  });

  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['id'] ?? 0,
      name: json['name'] ?? "",
      role: json['role'] ?? "",
      profilePicture: json['profile_picture'],
      profile: json['profile'],
    );
  }
}

// ------------------------------------------------------------
//                     PARENT TAG MODEL
// ------------------------------------------------------------
class ParentTagModel {
  final int id;
  final String message;
  final String type;
  final String? fileName;

  ParentTagModel({
    required this.id,
    required this.message,
    required this.type,
    this.fileName,
  });

  factory ParentTagModel.fromJson(Map<String, dynamic> json) {
    return ParentTagModel(
      id: json['id'] ?? 0,
      message: json['message'] ?? "",
      type: json['type'] ?? "",
      fileName: json['file_name'],
    );
  }
}
