class EmployeeModel {
  final String id;
  final String employeeId;
  final String name;
  final String designation;
  final DateTime? checkIn;
  final DateTime? checkOut;
  final String firstName;
  final String lastName;
  final int activity;
  int action;
  final String jobRole;
  final String email;
  final String phoneNumber;
  final String jobRoleID;

  EmployeeModel({
    required this.id,
    required this.jobRoleID,
    required this.employeeId,
    required this.name,
    required this.designation,
    this.checkIn,
    this.checkOut,
    required this.firstName,
    required this.lastName,
    required this.activity,
    required this.action,
    required this.jobRole,
    required this.email,
    required this.phoneNumber,
  });

  factory EmployeeModel.fromJson(Map<String, dynamic> json) {
    return EmployeeModel(
      id: json['id']?.toString() ?? '',
      employeeId: json['employeeId']?.toString() ?? '',
      jobRoleID: json['jobRoleId']?.toString() ?? '',
      name: '${json['firstName'] ?? ''} ${json['lastName'] ?? ''}'.trim(),
      designation: json['jobRoleName'] ?? '',
      checkIn: json['checkInTime'] != null
          ? DateTime.tryParse(json['checkInTime'])
          : null,
      checkOut: json['checkOutTime'] != null
          ? DateTime.tryParse(json['checkOutTime'])
          : null,
      action: json['action'] ?? 0,
      activity: json['activity'] ?? 0,
      jobRole: json['jobRole']?.toString() ?? '-',
      email: json['email']?.toString() ?? '-',
      phoneNumber: json['phoneNumber']?.toString() ?? '-',
      firstName: json['firstName'] ?? '',
      lastName: json['lastName'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'employeeId': employeeId,
      'firstName': name.split(' ').first,
      'lastName': name.split(' ').length > 1 ? name.split(' ').last : '',
      'jobRoleId': jobRoleID,
      'jobRoleName': designation,
      'checkInTime': checkIn?.toIso8601String(),
      'checkOutTime': checkOut?.toIso8601String(),
      'action': action,
      'activity': activity,
      'jobRole': jobRole.isEmpty ? '-' : jobRole,
      'email': email.isEmpty ? '-' : email,
      'phoneNumber': phoneNumber.isEmpty ? '-' : phoneNumber,
    };
  }

  /// ✅ Add equality based on unique `id`
  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is EmployeeModel && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}
