class DocumentsResponse {
  final bool success;
  final String message;
  final DocumentDataWrapper? data;
  final dynamic errors;
  final int statusCode;
  final DateTime timestamp;

  DocumentsResponse({
    required this.success,
    required this.message,
    this.data,
    this.errors,
    required this.statusCode,
    required this.timestamp,
  });

  factory DocumentsResponse.fromJson(Map<String, dynamic> json) {
    return DocumentsResponse(
      success: json['success'] ?? false,
      message: json['message'] ?? '',
      data: json['data'] != null
          ? DocumentDataWrapper.fromJson(json['data'])
          : null,
      errors: json['errors'],
      statusCode: json['statusCode'] ?? 0,
      timestamp: json['timestamp'] != null
          ? DateTime.tryParse(json['timestamp']) ?? DateTime.now()
          : DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'message': message,
      'data': data?.toJson(),
      'errors': errors,
      'statusCode': statusCode,
      'timestamp': timestamp.toIso8601String(),
    };
  }
}

class DocumentDataWrapper {
  final dynamic currentFilter;
  final int currentPage;
  final int totalPages;
  final int totalEntites;
  final List<DocumentItem> data;

  DocumentDataWrapper({
    this.currentFilter,
    required this.currentPage,
    required this.totalPages,
    required this.totalEntites,
    required this.data,
  });

  factory DocumentDataWrapper.fromJson(Map<String, dynamic> json) {
    return DocumentDataWrapper(
      currentFilter: json['currentFilter'],
      currentPage: json['currentPage'] ?? 0,
      totalPages: json['totalPages'] ?? 0,
      totalEntites: json['totalEntites'] ?? 0,
      data: (json['data'] as List<dynamic>?)
          ?.map((e) => DocumentItem.fromJson(e))
          .toList() ??
          [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'currentFilter': currentFilter,
      'currentPage': currentPage,
      'totalPages': totalPages,
      'totalEntites': totalEntites,
      'data': data.map((e) => e.toJson()).toList(),
    };
  }
}

class DocumentItem {
  final String id;
  final String name;
  final String documentId;
  final String description;
  final DateTime? uploadedAt;
  final String? parentAttachmentId;
  final bool isCurrentVersion;
  final int version;
  final bool isActive;
  final bool? isVerified;
  final UploadedBy? uploadedBy;
  final DocumentType? documentType;

  DocumentItem({
    required this.id,
    required this.name,
    required this.documentId,
    required this.description,
    this.uploadedAt,
    this.parentAttachmentId,
    required this.isCurrentVersion,
    required this.version,
    required this.isActive,
    this.isVerified,
    this.uploadedBy,
    this.documentType,
  });

  factory DocumentItem.fromJson(Map<String, dynamic> json) {
    return DocumentItem(
      id: json['id'] ?? '',
      name: json['name'] ?? '',
      documentId: json['documentId'] ?? '',
      description: json['description'] ?? '',
      uploadedAt: json['uploadedAt'] != null
          ? DateTime.tryParse(json['uploadedAt'])
          : null,
      parentAttachmentId: json['parentAttachmentId'],
      isCurrentVersion: json['isCurrentVersion'] ?? false,
      version: json['version'] ?? 0,
      isActive: json['isActive'] ?? false,
      isVerified: json['isVerified'],
      uploadedBy: json['uploadedBy'] != null
          ? UploadedBy.fromJson(json['uploadedBy'])
          : null,
      documentType: json['documentType'] != null
          ? DocumentType.fromJson(json['documentType'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'documentId': documentId,
      'description': description,
      'uploadedAt': uploadedAt?.toIso8601String(),
      'parentAttachmentId': parentAttachmentId,
      'isCurrentVersion': isCurrentVersion,
      'version': version,
      'isActive': isActive,
      'isVerified': isVerified,
      'uploadedBy': uploadedBy?.toJson(),
      'documentType': documentType?.toJson(),
    };
  }
}

extension DocumentItemCopy on DocumentItem {
  DocumentItem copyWith({
    bool? isActive,
  }) {
    return DocumentItem(
      id: id,
      name: name,
      documentId: documentId,
      description: description,
      uploadedAt: uploadedAt,
      parentAttachmentId: parentAttachmentId,
      isCurrentVersion: isCurrentVersion,
      version: version,
      isActive: isActive ?? this.isActive,
      isVerified: isVerified,
      uploadedBy: uploadedBy,
      documentType: documentType,
    );
  }
}

class UploadedBy {
  final String id;
  final String firstName;
  final String? lastName;
  final String? photo;
  final String jobRoleId;
  final String jobRoleName;

  UploadedBy({
    required this.id,
    required this.firstName,
    this.lastName,
    this.photo,
    required this.jobRoleId,
    required this.jobRoleName,
  });

  factory UploadedBy.fromJson(Map<String, dynamic> json) {
    return UploadedBy(
      id: json['id'] ?? '',
      firstName: json['firstName'] ?? '',
      lastName: json['lastName'],
      photo: json['photo'],
      jobRoleId: json['jobRoleId'] ?? '',
      jobRoleName: json['jobRoleName'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'firstName': firstName,
      'lastName': lastName,
      'photo': photo,
      'jobRoleId': jobRoleId,
      'jobRoleName': jobRoleName,
    };
  }
}

class DocumentType {
  final String id;
  final String name;
  final String? regexExpression;
  final String? allowedContentType;
  final double? maxSizeAllowedInMB;
  final bool isValidationRequired;
  final bool isMandatory;
  final bool isSystem;
  final bool isActive;
  final DocumentCategory? documentCategory;

  DocumentType({
    required this.id,
    required this.name,
    this.regexExpression,
    this.allowedContentType,
    this.maxSizeAllowedInMB,
    required this.isValidationRequired,
    required this.isMandatory,
    required this.isSystem,
    required this.isActive,
    this.documentCategory,
  });

  factory DocumentType.fromJson(Map<String, dynamic> json) {
    return DocumentType(
      id: json['id'] ?? '',
      name: json['name'] ?? '',
      regexExpression: json['regexExpression'],
      allowedContentType: json['allowedContentType'],
      maxSizeAllowedInMB: json['maxSizeAllowedInMB'],
      isValidationRequired: json['isValidationRequired'] ?? false,
      isMandatory: json['isMandatory'] ?? false,
      isSystem: json['isSystem'] ?? false,
      isActive: json['isActive'] ?? false,
      documentCategory: json['documentCategory'] != null
          ? DocumentCategory.fromJson(json['documentCategory'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'regexExpression': regexExpression,
      'allowedContentType': allowedContentType,
      'maxSizeAllowedInMB': maxSizeAllowedInMB,
      'isValidationRequired': isValidationRequired,
      'isMandatory': isMandatory,
      'isSystem': isSystem,
      'isActive': isActive,
      'documentCategory': documentCategory?.toJson(),
    };
  }
}

class DocumentCategory {
  final String id;
  final String name;
  final String? description;
  final String? entityTypeId;

  DocumentCategory({
    required this.id,
    required this.name,
    this.description,
    this.entityTypeId,
  });

  factory DocumentCategory.fromJson(Map<String, dynamic> json) {
    return DocumentCategory(
      id: json['id'] ?? '',
      name: json['name'] ?? '',
      description: json['description'],
      entityTypeId: json['entityTypeId'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'entityTypeId': entityTypeId,
    };
  }
}
