// To parse this JSON data, do
//
//     final document = documentFromJson(jsonString);

import 'dart:convert';

import 'package:mc_common_app/utils/enums.dart';

Document documentFromJson(String str) => Document.fromJson(json.decode(str));

String documentToJson(Document data) => json.encode(data.toJson());

class Document {
  Document({
    this.totalItemsCount,
    this.data,
    this.messageStatus,
    this.message,
  });

  int? totalItemsCount;
  List<DocumentData>? data;
  int? messageStatus;
  String? message;

  factory Document.fromJson(Map<String, dynamic> json) => Document(
        totalItemsCount: json["totalItemsCount"],
        data: json["data"] == null ? null : List<DocumentData>.from(json["data"].map((x) => DocumentData.fromJson(x))),
        messageStatus: json["messageStatus"],
        message: json["message"],
      );

  Map<String, dynamic> toJson() => {
        "totalItemsCount": totalItemsCount,
        "data": data == null ? null : List<dynamic>.from(data!.map((x) => x.toJson())),
        "messageStatus": messageStatus,
        "message": message,
      };
}

class DocumentData {
  DocumentData(
      {this.id,
      this.serviceProviderId,
      this.documentId,
      this.documentUrl,
      this.status,
      this.statusText,
      this.comment,
      this.isActive,
      this.document,
      this.fileExt,
      this.documentName,
      this.isLocalFile,
      this.description,
      this.dateExpire,
      this.isAllowUpdate,
      this.isExpired});

  int? id;
  int? serviceProviderId;
  int? documentId;
  String? documentUrl;
  DocumentStatusEnum? status;
  String? comment;
  bool? isActive;
  String? document;
  String? fileExt;
  String? statusText;
  String? documentName;
  bool? isLocalFile;
  String? description;
  String? dateExpire;
  bool? isExpired;
  bool? isAllowUpdate;

  factory DocumentData.fromJson(Map<String, dynamic> json) => DocumentData(
      id: json["id"],
      serviceProviderId: json["serviceProviderID"],
      documentId: json["documentID"],
      documentUrl: json["documentURL"],
      status: json.containsKey("status") ? (json['status'] as int).toDocumentStatusEnum() : null,
      statusText: json["statusText"],
      comment: json["comment"],
      isActive: json["isActive"],
      dateExpire: json["dateExpire"],
      isExpired: json["isExpired"],
      isAllowUpdate: json["isAllowUpdate"],
      document: null,
      fileExt: null,
      documentName: json["documentName"],
      isLocalFile: false,
      description: null);

  Map<String, dynamic> toJson() => {
        "id": id,
        "serviceProviderID": serviceProviderId,
        "documentID": documentId,
        "documentURL": documentUrl,
        "status": status,
        "comment": comment,
        "isActive": isActive,
        "dateExpire": dateExpire,
        "isExpired": isExpired,
        "isAllowUpdate": isAllowUpdate,
      };
}

extension DocumentEnum on int {
  DocumentStatusEnum toDocumentStatusEnum() {
    switch (this) {
      case 0:
        return DocumentStatusEnum.needUpload;
      case 1:
        return DocumentStatusEnum.pending;
      case 2:
        return DocumentStatusEnum.review;
      case 3:
        return DocumentStatusEnum.approvedOrActive;
        case 4:
        return DocumentStatusEnum.rejected;
      default:
        throw Exception('Invalid status value: $this'); // Explicit handling for invalid cases
    }
  }
}
