class FinancialTableEntry {
  final String monthYear;
  final double chitValue;
  final double bidAmount;
  final double subscriptionAmount;
  final double commissionInstallment;
  final double totalPayableInstallment;
  final double dividendAmount;

  FinancialTableEntry({
    required this.monthYear,
    required this.chitValue,
    required this.bidAmount,
    required this.subscriptionAmount,
    required this.commissionInstallment,
    required this.totalPayableInstallment,
    required this.dividendAmount,
  });

  factory FinancialTableEntry.fromJson(Map<String, dynamic> json) {
    return FinancialTableEntry(
      monthYear: json['month_year'] ?? '',
      chitValue: _parseDouble(json['chit_value']),
      bidAmount: _parseDouble(json['bid_amount']),
      subscriptionAmount: _parseDouble(json['subscription_amount']),
      commissionInstallment: _parseDouble(json['commission_installment']),
      totalPayableInstallment: _parseDouble(json['total_payable_installment']),
      dividendAmount: _parseDouble(json['dividend_amount']),
    );
  }

  static double _parseDouble(dynamic value) {
    if (value == null) return 0.0;
    if (value is double) return value;
    if (value is int) return value.toDouble();
    if (value is String) {
      try {
        return double.parse(value);
      } catch (e) {
        print('Error parsing string to double: $value, error: $e');
        return 0.0;
      }
    }
    print('Warning: Cannot parse $value (${value.runtimeType}) to double, returning 0.0');
    return 0.0;
  }

  Map<String, dynamic> toJson() {
    return {
      'month_year': monthYear,
      'chit_value': chitValue,
      'bid_amount': bidAmount,
      'subscription_amount': subscriptionAmount,
      'commission_installment': commissionInstallment,
      'total_payable_installment': totalPayableInstallment,
      'dividend_amount': dividendAmount,
    };
  }

  // Helper method to check if this is a total row
  bool get isTotal => monthYear.toLowerCase() == 'total';

  // Helper method to get color for dividend amount
  bool get isDividendNegative => dividendAmount < 0;
}
