import '../../domain/entities/trade_order.dart';
import '../../domain/entities/trade_record.dart';

class TradeOrderModel extends TradeOrder {
  const TradeOrderModel({
    required super.id,
    required super.orderNo,
    required super.orderType,
    required super.status,
    required super.price,
    required super.quantity,
    required super.filledQuantity,
    required super.remainingQuantity,
    required super.averagePrice,
    required super.totalAmount,
    required super.createdAt,
    super.completedAt,
    super.cancelledAt,
  });

  factory TradeOrderModel.fromJson(Map<String, dynamic> json) {
    return TradeOrderModel(
      id: json['id']?.toString() ?? '',
      orderNo: json['orderNo']?.toString() ?? '',
      orderType: _parseOrderType(json['type']),
      status: _parseStatus(json['status']),
      price: json['price']?.toString() ?? '0',
      quantity: json['quantity']?.toString() ?? '0',
      filledQuantity: json['filledQuantity']?.toString() ?? '0',
      remainingQuantity: json['remainingQuantity']?.toString() ?? '0',
      averagePrice: json['averagePrice']?.toString() ?? '0',
      totalAmount: json['totalAmount']?.toString() ?? '0',
      createdAt: json['createdAt'] != null
          ? DateTime.parse(json['createdAt'].toString())
          : DateTime.now(),
      completedAt: json['completedAt'] != null
          ? DateTime.parse(json['completedAt'].toString())
          : null,
      cancelledAt: json['cancelledAt'] != null
          ? DateTime.parse(json['cancelledAt'].toString())
          : null,
    );
  }

  static OrderType _parseOrderType(String? type) {
    switch (type) {
      case 'BUY':
        return OrderType.buy;
      case 'SELL':
        return OrderType.sell;
      default:
        return OrderType.buy;
    }
  }

  static OrderStatus _parseStatus(String? status) {
    switch (status) {
      case 'PENDING':
        return OrderStatus.pending;
      case 'PARTIAL':
        return OrderStatus.partial;
      case 'FILLED':
        return OrderStatus.filled;
      case 'CANCELLED':
        return OrderStatus.cancelled;
      default:
        return OrderStatus.pending;
    }
  }
}

/// 订单列表响应
class OrdersPageModel {
  final List<TradeOrderModel> data;
  final int total;

  const OrdersPageModel({
    required this.data,
    required this.total,
  });

  factory OrdersPageModel.fromJson(Map<String, dynamic> json) {
    final dataList = (json['data'] as List<dynamic>?) ?? [];
    return OrdersPageModel(
      data: dataList.map((e) => TradeOrderModel.fromJson(e)).toList(),
      total: json['total'] ?? 0,
    );
  }
}

/// 成交记录 Model（包含完整交易明细）
class TradeRecordModel extends TradeRecord {
  const TradeRecordModel({
    required super.id,
    required super.tradeNo,
    required super.type,
    required super.price,
    required super.quantity,
    required super.originalQuantity,
    required super.burnQuantity,
    required super.burnMultiplier,
    required super.grossAmount,
    required super.fee,
    required super.netAmount,
    required super.counterparty,
    required super.createdAt,
  });

  factory TradeRecordModel.fromJson(Map<String, dynamic> json) {
    return TradeRecordModel(
      id: json['id']?.toString() ?? '',
      tradeNo: json['tradeNo']?.toString() ?? '',
      type: json['type'] == 'BUY' ? TradeType.buy : TradeType.sell,
      price: json['price']?.toString() ?? '0',
      quantity: json['quantity']?.toString() ?? '0',
      originalQuantity: json['originalQuantity']?.toString() ?? json['quantity']?.toString() ?? '0',
      burnQuantity: json['burnQuantity']?.toString() ?? '0',
      burnMultiplier: json['burnMultiplier']?.toString() ?? '1',
      grossAmount: json['grossAmount']?.toString() ?? '0',
      fee: json['fee']?.toString() ?? '0',
      netAmount: json['netAmount']?.toString() ?? '0',
      counterparty: json['counterparty']?.toString() ?? '',
      createdAt: json['createdAt'] != null
          ? DateTime.parse(json['createdAt'].toString())
          : DateTime.now(),
    );
  }
}

/// 成交记录列表响应
class TradesPageModel {
  final List<TradeRecordModel> data;
  final int total;

  const TradesPageModel({
    required this.data,
    required this.total,
  });

  factory TradesPageModel.fromJson(Map<String, dynamic> json) {
    final dataList = (json['data'] as List<dynamic>?) ?? [];
    return TradesPageModel(
      data: dataList.map((e) => TradeRecordModel.fromJson(e)).toList(),
      total: json['total'] ?? 0,
    );
  }
}
