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

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

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

class DocumentDetails {
  final String id;
  final String name;
  final String documentId;
  final String? description;
  final int version;
  final bool isCurrentVersion;
  final String parentAttachmentId;
  final DateTime uploadedAt;
  final UploadedBy uploadedBy;
  final DateTime? updatedAt;
  final UploadedBy? updatedBy;
  final DateTime? verifiedAt;
  final bool? isVerified;
  final UploadedBy? verifiedBy;
  final String entityId;
  final DocumentType documentType;
  final List<DocumentTag> tags;
  final bool isActive;

  DocumentDetails({
    required this.id,
    required this.name,
    required this.documentId,
    this.description,
    required this.version,
    required this.isCurrentVersion,
    required this.parentAttachmentId,
    required this.uploadedAt,
    required this.uploadedBy,
    this.updatedAt,
    this.updatedBy,
    this.verifiedAt,
    this.isVerified,
    this.verifiedBy,
    required this.entityId,
    required this.documentType,
    required this.tags,
    required this.isActive,
  });

  factory DocumentDetails.fromJson(Map<String, dynamic> json) {
    return DocumentDetails(
      id: json['id'] ?? '',
      name: json['name'] ?? '',
      documentId: json['documentId'] ?? '',
      description: json['description'],
      version: json['version'] ?? 0,
      isCurrentVersion: json['isCurrentVersion'] ?? false,
      parentAttachmentId: json['parentAttachmentId'] ?? '',
      uploadedAt: DateTime.tryParse(json['uploadedAt'] ?? '') ?? DateTime.now(),
      uploadedBy: UploadedBy.fromJson(json['uploadedBy'] ?? {}),
      updatedAt: json['updatedAt'] != null
          ? DateTime.tryParse(json['updatedAt'])
          : null,
      updatedBy: json['updatedBy'] != null
          ? UploadedBy.fromJson(json['updatedBy'])
          : null,
      verifiedAt: json['verifiedAt'] != null
          ? DateTime.tryParse(json['verifiedAt'])
          : null,
      isVerified: json['isVerified'],
      verifiedBy: json['verifiedBy'] != null
          ? UploadedBy.fromJson(json['verifiedBy'])
          : null,
      entityId: json['entityId'] ?? '',
      documentType: DocumentType.fromJson(json['documentType'] ?? {}),
      tags: (json['tags'] as List<dynamic>?)
              ?.map((tag) => DocumentTag.fromJson(tag))
              .toList() ??
          [],
      isActive: json['isActive'] ?? false,
    );
  }
}

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,
    required this.allowedContentType,
    required 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'] ?? 0,
      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,
    required 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,
    };
  }
}

class DocumentTag {
  final String name;
  final bool isActive;

  DocumentTag({required this.name, required this.isActive});

  factory DocumentTag.fromJson(Map<String, dynamic> json) {
    return DocumentTag(
      name: json['name'] ?? '',
      isActive: json['isActive'] ?? false,
    );
  }

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