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

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

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

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

class ProjectData {
  final int currentPage;
  final int totalPages;
  final int totalEntities;
  final List<ProjectItem> data;

  ProjectData({
    required this.currentPage,
    required this.totalPages,
    required this.totalEntities,
    required this.data,
  });

  factory ProjectData.fromJson(Map<String, dynamic> json) {
    return ProjectData(
      currentPage: json['currentPage'] ?? 1,
      totalPages: json['totalPages'] ?? 1,
      totalEntities: json['totalEntites'] ?? 0,
      data: json['data'] != null
          ? List<ProjectItem>.from(
              json['data'].map((x) => ProjectItem.fromJson(x)))
          : [],
    );
  }

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

class ProjectItem {
  final String id;
  final String name;
  final String shortName;
  final String address;
  final DateTime assignedDate;
  final Status? status;
  final Client? client;
  final List<Service> services;
  final String contactName;
  final String contactPhone;
  final String contactEmail;
  final DateTime createdAt;
  final CreatedBy? createdBy;

  ProjectItem({
    required this.id,
    required this.name,
    required this.shortName,
    required this.address,
    required this.assignedDate,
    this.status,
    this.client,
    required this.services,
    required this.contactName,
    required this.contactPhone,
    required this.contactEmail,
    required this.createdAt,
    this.createdBy,
  });

  factory ProjectItem.fromJson(Map<String, dynamic> json) {
    return ProjectItem(
      id: json['id'] ?? '',
      name: json['name'] ?? '',
      shortName: json['shortName'] ?? '',
      address: json['address'] ?? '',
      assignedDate:
          DateTime.tryParse(json['assignedDate'] ?? '') ?? DateTime.now(),
      status: json['status'] != null ? Status.fromJson(json['status']) : null,
      client: json['client'] != null ? Client.fromJson(json['client']) : null,
      services: json['services'] != null
          ? List<Service>.from(json['services'].map((x) => Service.fromJson(x)))
          : [],
      contactName: json['contactName'] ?? '',
      contactPhone: json['contactPhone'] ?? '',
      contactEmail: json['contactEmail'] ?? '',
      createdAt: DateTime.tryParse(json['createdAt'] ?? '') ?? DateTime.now(),
      createdBy: json['createdBy'] != null
          ? CreatedBy.fromJson(json['createdBy'])
          : null,
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'name': name,
        'shortName': shortName,
        'address': address,
        'assignedDate': assignedDate.toIso8601String(),
        'status': status?.toJson(),
        'client': client?.toJson(),
        'services': services.map((x) => x.toJson()).toList(),
        'contactName': contactName,
        'contactPhone': contactPhone,
        'contactEmail': contactEmail,
        'createdAt': createdAt.toIso8601String(),
        'createdBy': createdBy?.toJson(),
      };
}

class Status {
  final String id;
  final String status;

  Status({
    required this.id,
    required this.status,
  });

  factory Status.fromJson(Map<String, dynamic> json) {
    return Status(
      id: json['id'] ?? '',
      status: json['status'] ?? '',
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'status': status,
      };
}

class Client {
  final String id;
  final String name;
  final String email;
  final String contactPerson;
  final String address;
  final String contactNumber;
  final int sprid;

  Client({
    required this.id,
    required this.name,
    required this.email,
    required this.contactPerson,
    required this.address,
    required this.contactNumber,
    required this.sprid,
  });

  factory Client.fromJson(Map<String, dynamic> json) {
    return Client(
      id: json['id'] ?? '',
      name: json['name'] ?? '',
      email: json['email'] ?? '',
      contactPerson: json['contactPerson'] ?? '',
      address: json['address'] ?? '',
      contactNumber: json['contactNumber'] ?? '',
      sprid: (json['sprid'] is int)
          ? json['sprid']
          : (json['sprid'] is double)
              ? (json['sprid'] as double).toInt()
              : int.tryParse(json['sprid']?.toString() ?? "0") ?? 0,
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'name': name,
        'email': email,
        'contactPerson': contactPerson,
        'address': address,
        'contactNumber': contactNumber,
        'sprid': sprid,
      };
}

class Service {
  final String id;
  final String name;
  final String description;
  final bool isSystem;
  final bool isActive;

  Service({
    required this.id,
    required this.name,
    required this.description,
    required this.isSystem,
    required this.isActive,
  });

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

  Map<String, dynamic> toJson() => {
        'id': id,
        'name': name,
        'description': description,
        'isSystem': isSystem,
        'isActive': isActive,
      };
}

class CreatedBy {
  final String id;
  final String firstName;
  final String lastName;
  final String email;
  final String photo;
  final String jobRoleId;
  final String jobRoleName;

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

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

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