enum EntryType { work, leave, wfh }

enum LeaveType { sick, annual, nationalHoliday, companyWfh }

class AttendanceEntry {
  final int? id;
  final DateTime date;
  final DateTime? loginTime;
  final DateTime? logoutTime;
  final String? notes;
  final EntryType entryType;
  final LeaveType? leaveType;

  AttendanceEntry({
    this.id,
    required this.date,
    this.loginTime,
    this.logoutTime,
    this.notes,
    this.entryType = EntryType.work,
    this.leaveType,
  });

  // Helper method to create a copy of the object with some fields updated.
  AttendanceEntry copyWith({
    int? id,
    DateTime? date,
    DateTime? loginTime,
    DateTime? logoutTime,
    String? notes,
    EntryType? entryType,
    LeaveType? leaveType,
  }) {
    return AttendanceEntry(
      id: id ?? this.id,
      date: date ?? this.date,
      loginTime: loginTime ?? this.loginTime,
      logoutTime: logoutTime ?? this.logoutTime,
      notes: notes ?? this.notes,
      entryType: entryType ?? this.entryType,
      leaveType: leaveType ?? this.leaveType,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'date': date.toIso8601String().substring(0, 10),
      'loginTime': loginTime?.toIso8601String(),
      'logoutTime': logoutTime?.toIso8601String(),
      'notes': notes,
      'entryType': entryType.name,
      'leaveType': leaveType?.name,
    };
  }

  factory AttendanceEntry.fromMap(Map<String, dynamic> map) {
    // Check for null 'id' before casting
    final int? id = map['id'] as int?;

    return AttendanceEntry(
      id: id,
      date: DateTime.parse(map['date'] as String),
      loginTime: map['loginTime'] != null
          ? DateTime.parse(map['loginTime'] as String)
          : null,
      logoutTime: map['logoutTime'] != null
          ? DateTime.parse(map['logoutTime'] as String)
          : null,
      notes: map['notes'] as String?,
      entryType: EntryType.values.firstWhere(
        (e) => e.name == map['entryType'],
        orElse: () => EntryType.work,
      ),
      leaveType: map['leaveType'] != null
          ? LeaveType.values.firstWhere((e) => e.name == map['leaveType'])
          : null,
    );
  }
}
 