class AdvanceResponseDto {
  final int statusCode;
  final bool isSuccess;
  final String? message;
  final AdvanceDataDto? data;

  AdvanceResponseDto({
    required this.statusCode,
    required this.isSuccess,
    this.message,
    this.data,
  });

  factory AdvanceResponseDto.fromJson(Map<String, dynamic> json) {
    return AdvanceResponseDto(
      statusCode: json['statusCode'] ?? 0,
      isSuccess: json['isSuccess'] ?? false,
      message: json['message'],
      data: json['data'] != null ? AdvanceDataDto.fromJson(json['data']) : null,
    );
  }
}

class AdvanceDataDto {
  final String employeeId;
  final String? employeeFullName;
  final DateTime date;
  final double amount;
  final String? submittedBy;
  final String? submittedByUser;
  final String reason;
  final int state;
  final String id;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final DateTime? deletedAt;
  final bool? isDeleted;

  AdvanceDataDto({
    required this.employeeId,
    this.employeeFullName,
    required this.date,
    required this.amount,
    this.submittedBy,
    this.submittedByUser,
    required this.reason,
    required this.state,
    required this.id,
    this.createdAt,
    this.updatedAt,
    this.deletedAt,
    this.isDeleted,
  });

  factory AdvanceDataDto.fromJson(Map<String, dynamic> json) {
    return AdvanceDataDto(
      employeeId: json['employeeId']?.toString() ?? '',
      employeeFullName: json['employeeFullName'],
      date: _parseDateTime(json['date'])!,
      amount: (json['amount'] is int) ? (json['amount'] as int).toDouble() : (json['amount'] as num).toDouble(),
      submittedBy: json['submittedBy'],
      submittedByUser: json['submittedByUser'],
      reason: json['reason']?.toString() ?? '',
      state: json['state'] ?? 0,
      id: json['id']?.toString() ?? '',
      createdAt: _parseDateTime(json['createdAt']),
      updatedAt: _parseDateTime(json['updatedAt']),
      deletedAt: _parseDateTime(json['deletedAt']),
      isDeleted: json['isDeleted'],
    );
  }

  static DateTime? _parseDateTime(dynamic value) {
    if (value == null) return null;
    if (value is DateTime) return value;
    if (value is String) {
      try {
        return DateTime.parse(value);
      } catch (e) {
        print('Error parsing date: $value - $e');
        return null;
      }
    }
    return null;
  }
}
