class ExpenseTypeReportResponse {
  final bool success;
  final String message;
  final ExpenseTypeReportData data;
  final dynamic errors;
  final int statusCode;
  final DateTime timestamp;

  ExpenseTypeReportResponse({
    required this.success,
    required this.message,
    required this.data,
    this.errors,
    required this.statusCode,
    required this.timestamp,
  });

  factory ExpenseTypeReportResponse.fromJson(Map<String, dynamic> json) {
    return ExpenseTypeReportResponse(
      success: json['success'] ?? false,
      message: json['message'] ?? '',
      data: ExpenseTypeReportData.fromJson(json['data'] ?? {}),
      errors: json['errors'],
      statusCode: json['statusCode'] ?? 0,
      timestamp: DateTime.parse(json['timestamp']),
    );
  }

  Map<String, dynamic> toJson() => {
        'success': success,
        'message': message,
        'data': data.toJson(),
        'errors': errors,
        'statusCode': statusCode,
        'timestamp': timestamp.toIso8601String(),
      };
}

class ExpenseTypeReportData {
  final List<ExpenseTypeReportItem> report;
  final double totalAmount;

  ExpenseTypeReportData({
    required this.report,
    required this.totalAmount,
  });

  factory ExpenseTypeReportData.fromJson(Map<String, dynamic> json) {
    return ExpenseTypeReportData(
      report: json['report'] != null
          ? List<ExpenseTypeReportItem>.from(
              json['report'].map((x) => ExpenseTypeReportItem.fromJson(x)))
          : [],
      totalAmount: json['totalAmount'] != null
          ? (json['totalAmount'] is int
              ? (json['totalAmount'] as int).toDouble()
              : json['totalAmount'] as double)
          : 0.0,
    );
  }

  Map<String, dynamic> toJson() => {
        'report': report.map((x) => x.toJson()).toList(),
        'totalAmount': totalAmount,
      };
}

class ExpenseTypeReportItem {
  final String projectName;
  final double totalApprovedAmount;
  final double totalPendingAmount;
  final double totalRejectedAmount;
  final double totalProcessedAmount;

  ExpenseTypeReportItem({
    required this.projectName,
    required this.totalApprovedAmount,
    required this.totalPendingAmount,
    required this.totalRejectedAmount,
    required this.totalProcessedAmount,
  });

  factory ExpenseTypeReportItem.fromJson(Map<String, dynamic> json) {
    double parseAmount(dynamic value) {
      if (value == null) return 0.0;
      return value is int ? value.toDouble() : value as double;
    }

    return ExpenseTypeReportItem(
      projectName: json['projectName'] ?? '',
      totalApprovedAmount: parseAmount(json['totalApprovedAmount']),
      totalPendingAmount: parseAmount(json['totalPendingAmount']),
      totalRejectedAmount: parseAmount(json['totalRejectedAmount']),
      totalProcessedAmount: parseAmount(json['totalProcessedAmount']),
    );
  }

  Map<String, dynamic> toJson() => {
        'projectName': projectName,
        'totalApprovedAmount': totalApprovedAmount,
        'totalPendingAmount': totalPendingAmount,
        'totalRejectedAmount': totalRejectedAmount,
        'totalProcessedAmount': totalProcessedAmount,
      };
}
