class AttendanceRecordDto {
  final String id;
  final String employeeId;
  final DateTime? login;
  final DateTime? logout;
  final String? reason;
  final DateTime? createdAt;
  final bool isDeleted;

  AttendanceRecordDto({
    required this.id,
    required this.employeeId,
    this.login,
    this.logout,
    this.reason,
    this.createdAt,
    required this.isDeleted,
  });

  factory AttendanceRecordDto.fromJson(Map<String, dynamic> json) {
    return AttendanceRecordDto(
      id: json['id']?.toString() ?? '',
      employeeId: json['employeeId']?.toString() ?? '',
      login: _parseDateTime(json['login']),
      logout: _parseDateTime(json['logout']),
      reason: json['reason'],
      createdAt: _parseDateTime(json['createdAt']),
      isDeleted: json['isDeleted'] ?? false,
    );
  }

  static DateTime? _parseDateTime(dynamic value) {
    if (value == null) return null;
    if (value is DateTime) return value;
    if (value is String) {
      if (value.isEmpty) return null;
      try {
        return DateTime.parse(value);
      } catch (e) {
        return null; // Handle parse error gracefully
      }
    }
    return null;
  }
}
