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

class TradeOrderModel extends TradeOrder {
  const TradeOrderModel({
    required super.id,
    required super.accountSequence,
    required super.orderType,
    required super.status,
    required super.price,
    required super.quantity,
    required super.filledQuantity,
    required super.totalAmount,
    required super.createdAt,
    super.updatedAt,
  });

  factory TradeOrderModel.fromJson(Map<String, dynamic> json) {
    return TradeOrderModel(
      id: json['id'] ?? '',
      accountSequence: json['accountSequence']?.toString() ?? '',
      orderType: json['orderType'] == 'BUY' ? OrderType.buy : OrderType.sell,
      status: _parseStatus(json['status']),
      price: json['price']?.toString() ?? '0',
      quantity: json['quantity']?.toString() ?? '0',
      filledQuantity: json['filledQuantity']?.toString() ?? '0',
      totalAmount: json['totalAmount']?.toString() ?? '0',
      createdAt: json['createdAt'] != null
          ? DateTime.parse(json['createdAt'])
          : DateTime.now(),
      updatedAt: json['updatedAt'] != null
          ? DateTime.parse(json['updatedAt'])
          : null,
    );
  }

  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;
    }
  }
}
