import '../utils/formatters.dart';

/// 券详情
class CouponDetail {
  final BigInt tokenId;
  final BigInt faceValue;
  final int couponType; // 0=utility, 1=security
  final DateTime expiryDate;
  final int resaleCount;
  final int maxResaleCount;
  final bool transferable;
  final String issuer;
  final bool redeemed;

  CouponDetail({
    required this.tokenId,
    required this.faceValue,
    required this.couponType,
    required this.expiryDate,
    required this.resaleCount,
    required this.maxResaleCount,
    required this.transferable,
    required this.issuer,
    required this.redeemed,
  });

  /// 从合约 ABI 返回结果解析
  factory CouponDetail.fromAbiResult(String hexData, BigInt tokenId) {
    // getConfig 返回: (uint256 faceValue, uint8 couponType, uint256 expiry,
    //   uint256 resaleCount, uint256 maxResale, bool transferable, address issuer, bool redeemed)
    final data = hexData.startsWith('0x') ? hexData.substring(2) : hexData;
    if (data.length < 512) {
      return CouponDetail(
        tokenId: tokenId,
        faceValue: BigInt.zero,
        couponType: 0,
        expiryDate: DateTime.now(),
        resaleCount: 0,
        maxResaleCount: 0,
        transferable: false,
        issuer: '0x${'0' * 40}',
        redeemed: false,
      );
    }

    final faceValue = Formatters.hexToBigInt('0x${data.substring(0, 64)}');
    final couponType = Formatters.hexToInt('0x${data.substring(64, 128)}');
    final expiryTs = Formatters.hexToInt('0x${data.substring(128, 192)}');
    final resaleCount = Formatters.hexToInt('0x${data.substring(192, 256)}');
    final maxResale = Formatters.hexToInt('0x${data.substring(256, 320)}');
    final transferable = Formatters.hexToInt('0x${data.substring(320, 384)}') != 0;
    final issuer = '0x${data.substring(408, 448)}';
    final redeemed = Formatters.hexToInt('0x${data.substring(448, 512)}') != 0;

    return CouponDetail(
      tokenId: tokenId,
      faceValue: faceValue,
      couponType: couponType,
      expiryDate: DateTime.fromMillisecondsSinceEpoch(expiryTs * 1000),
      resaleCount: resaleCount,
      maxResaleCount: maxResale,
      transferable: transferable,
      issuer: issuer,
      redeemed: redeemed,
    );
  }

  factory CouponDetail.fromJson(Map<String, dynamic> json) {
    return CouponDetail(
      tokenId: BigInt.parse(json['tokenId'].toString()),
      faceValue: BigInt.parse(json['faceValue'].toString()),
      couponType: json['couponType'] as int,
      expiryDate: DateTime.parse(json['expiryDate'] as String),
      resaleCount: json['resaleCount'] as int,
      maxResaleCount: json['maxResaleCount'] as int,
      transferable: json['transferable'] as bool,
      issuer: json['issuer'] as String,
      redeemed: json['redeemed'] as bool,
    );
  }

  Map<String, dynamic> toJson() => {
        'tokenId': tokenId.toString(),
        'faceValue': faceValue.toString(),
        'couponType': couponType,
        'expiryDate': expiryDate.toIso8601String(),
        'resaleCount': resaleCount,
        'maxResaleCount': maxResaleCount,
        'transferable': transferable,
        'issuer': issuer,
        'redeemed': redeemed,
      };

  bool get isExpired => DateTime.now().isAfter(expiryDate);
  bool get isUtility => couponType == 0;
  bool get isSecurity => couponType == 1;
  bool get canResale => resaleCount < maxResaleCount && transferable && !redeemed;
}
