class ServiceProjectAllocationResponse {
  final bool success;
  final String message;
  final List<ServiceProjectAllocation> data;
  final dynamic errors;
  final int statusCode;
  final DateTime timestamp;

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

  factory ServiceProjectAllocationResponse.fromJson(Map<String, dynamic>? json) {
    if (json == null) {
      return ServiceProjectAllocationResponse(
        success: false,
        message: "",
        data: [],
        statusCode: 0,
        timestamp: DateTime.now(),
      );
    }

    return ServiceProjectAllocationResponse(
      success: json['success'] as bool? ?? false,
      message: json['message'] as String? ?? "",
      data: (json['data'] as List<dynamic>? ?? [])
          .map((e) => ServiceProjectAllocation.fromJson(e))
          .toList(),
      errors: json['errors'],
      statusCode: json['statusCode'] as int? ?? 0,
      timestamp: DateTime.tryParse(json['timestamp'] ?? "") ?? DateTime.now(),
    );
  }

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

class ServiceProjectAllocation {
  final String id;
  final Project project;
  final Employee employee;
  final TeamRole teamRole;
  final bool isActive;
  final DateTime assignedAt;
  final Employee assignedBy;
  final DateTime? reAssignedAt;
  final Employee? reAssignedBy;

  ServiceProjectAllocation({
    required this.id,
    required this.project,
    required this.employee,
    required this.teamRole,
    required this.isActive,
    required this.assignedAt,
    required this.assignedBy,
    this.reAssignedAt,
    this.reAssignedBy,
  });

  factory ServiceProjectAllocation.fromJson(Map<String, dynamic>? json) {
    if (json == null) {
      return ServiceProjectAllocation(
        id: "",
        project: Project.fromJson(null),
        employee: Employee.fromJson(null),
        teamRole: TeamRole.fromJson(null),
        isActive: false,
        assignedAt: DateTime.now(),
        assignedBy: Employee.fromJson(null),
      );
    }

    return ServiceProjectAllocation(
      id: json['id'] as String? ?? "",
      project: Project.fromJson(json['project']),
      employee: Employee.fromJson(json['employee']),
      teamRole: TeamRole.fromJson(json['teamRole']),
      isActive: json['isActive'] as bool? ?? false,
      assignedAt:
          DateTime.tryParse(json['assignedAt'] ?? "") ?? DateTime.now(),
      assignedBy: Employee.fromJson(json['assignedBy']),
      reAssignedAt: json['reAssignedAt'] != null
          ? DateTime.tryParse(json['reAssignedAt']) ?? DateTime.now()
          : null,
      reAssignedBy: json['reAssignedBy'] != null
          ? Employee.fromJson(json['reAssignedBy'])
          : null,
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'project': project.toJson(),
        'employee': employee.toJson(),
        'teamRole': teamRole.toJson(),
        'isActive': isActive,
        'assignedAt': assignedAt.toIso8601String(),
        'assignedBy': assignedBy.toJson(),
        'reAssignedAt': reAssignedAt?.toIso8601String(),
        'reAssignedBy': reAssignedBy?.toJson(),
      };
}

class Project {
  final String id;
  final String name;
  final String shortName;
  final DateTime assignedDate;
  final String contactName;
  final String contactPhone;
  final String contactEmail;

  Project({
    required this.id,
    required this.name,
    required this.shortName,
    required this.assignedDate,
    required this.contactName,
    required this.contactPhone,
    required this.contactEmail,
  });

  factory Project.fromJson(Map<String, dynamic>? json) {
    if (json == null) {
      return Project(
        id: "",
        name: "",
        shortName: "",
        assignedDate: DateTime.now(),
        contactName: "",
        contactPhone: "",
        contactEmail: "",
      );
    }

    return Project(
      id: json['id'] as String? ?? "",
      name: json['name'] as String? ?? "",
      shortName: json['shortName'] as String? ?? "",
      assignedDate:
          DateTime.tryParse(json['assignedDate'] ?? "") ?? DateTime.now(),
      contactName: json['contactName'] as String? ?? "",
      contactPhone: json['contactPhone'] as String? ?? "",
      contactEmail: json['contactEmail'] as String? ?? "",
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'name': name,
        'shortName': shortName,
        'assignedDate': assignedDate.toIso8601String(),
        'contactName': contactName,
        'contactPhone': contactPhone,
        'contactEmail': contactEmail,
      };
}

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

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

  factory Employee.fromJson(Map<String, dynamic>? json) {
    if (json == null) {
      return Employee(
        id: "",
        firstName: "",
        lastName: "",
        email: null,
        photo: null,
        jobRoleId: "",
        jobRoleName: "",
      );
    }

    return Employee(
      id: json['id'] as String? ?? "",
      firstName: json['firstName'] as String? ?? "",
      lastName: json['lastName'] as String? ?? "",
      email: json['email'] as String?,
      photo: json['photo'] as String?,
      jobRoleId: json['jobRoleId'] as String? ?? "",
      jobRoleName: json['jobRoleName'] as String? ?? "",
    );
  }

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

class TeamRole {
  final String id;
  final String name;
  final String description;

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

  factory TeamRole.fromJson(Map<String, dynamic>? json) {
    if (json == null) {
      return TeamRole(
        id: "",
        name: "",
        description: "",
      );
    }

    return TeamRole(
      id: json['id'] as String? ?? "",
      name: json['name'] as String? ?? "",
      description: json['description'] as String? ?? "",
    );
  }

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