class EarningModel {
  EarningModel({
    required this.id,
    required this.rate,
    required this.businessName,
    required this.businessAvatar,
    required this.businessSkill,
    required this.workHours,
    required this.amount,
    required this.status,
    required this.paidAt,
    required this.clockInAt,
    required this.clockOutAt,
    this.breakIn,
    this.breakOut,
    required this.eventName,
    required this.eventDate,
  });

  factory EarningModel.fromJson(Map<String, dynamic> json) {
    final assignment = json['assignment'] as Map<String, dynamic>;
    final positionData = assignment['position'] as Map<String, dynamic>;
    final eventData = positionData['shift']['event'] as Map<String, dynamic>;
    final businessData = eventData['business'] as Map<String, dynamic>;

    return EarningModel(
      id: json['id'] as String? ?? '',
      rate: (json['rate'] as num? ?? 0).toDouble(),
      businessName: businessData['name'] as String? ?? '',
      businessAvatar: businessData['avatar'] as String? ?? '',
      businessSkill:
          positionData['business_skill']['skill']['name'] as String? ?? '',
      workHours: (json['work_hours'] as num? ?? 0).toDouble(),
      amount: (json['amount'] as num? ?? 0).toDouble(),
      status: json['status'] as String? ?? 'failed',
      paidAt: DateTime.tryParse(
        json['paid_at'] as String? ?? '',
      ),
      clockInAt: DateTime.parse(
        assignment['clock_in'] ?? assignment['start_at'] as String,
      ),
      clockOutAt: DateTime.parse(
        assignment['clock_out'] ?? assignment['end_at'] as String,
      ),
      breakIn: DateTime.tryParse(assignment['break_in'] ?? ''),
      breakOut: DateTime.tryParse(assignment['break_out'] ?? ''),
      eventName: eventData['name'] as String? ?? '',
      eventDate: DateTime.parse(eventData['date'] as String? ?? ''),
    );
  }

  final String id;
  final double rate;
  final String businessName;
  final String businessAvatar;
  final String businessSkill;
  final double workHours;
  final double amount;
  final String status;
  final DateTime? paidAt;
  final DateTime clockInAt;
  final DateTime clockOutAt;
  final DateTime? breakIn;
  final DateTime? breakOut;
  final String eventName;
  final DateTime eventDate;
}
