import '../../domain/entities/share_account.dart';

class ShareAccountModel extends ShareAccount {
  const ShareAccountModel({
    required super.accountSequence,
    required super.miningBalance,
    required super.tradingBalance,
    required super.frozenBalance,
    required super.totalMined,
    required super.perSecondEarning,
    required super.effectiveContribution,
    super.firstMiningDate,
  });

  factory ShareAccountModel.fromJson(Map<String, dynamic> json) {
    return ShareAccountModel(
      accountSequence: json['accountSequence']?.toString() ?? '',
      // 后端返回 availableBalance，映射到 miningBalance
      miningBalance: json['availableBalance']?.toString() ?? json['miningBalance']?.toString() ?? '0',
      // 后端返回 totalBalance，映射到 tradingBalance（可交易余额）
      tradingBalance: json['totalBalance']?.toString() ?? json['tradingBalance']?.toString() ?? '0',
      frozenBalance: json['frozenBalance']?.toString() ?? '0',
      totalMined: json['totalMined']?.toString() ?? '0',
      perSecondEarning: json['perSecondEarning']?.toString() ?? '0',
      // 后端返回 totalContribution，映射到 effectiveContribution
      effectiveContribution: json['totalContribution']?.toString() ?? json['effectiveContribution']?.toString() ?? '0',
      firstMiningDate: json['firstMiningDate'] != null
          ? DateTime.tryParse(json['firstMiningDate'].toString())
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'accountSequence': accountSequence,
      'miningBalance': miningBalance,
      'tradingBalance': tradingBalance,
      'frozenBalance': frozenBalance,
      'totalMined': totalMined,
      'perSecondEarning': perSecondEarning,
      'effectiveContribution': effectiveContribution,
      'firstMiningDate': firstMiningDate?.toIso8601String(),
    };
  }
}
