/// Domain Entity: Cart Item
///
/// Represents a single item in a shopping cart.
library;

/// Cart Item Entity
///
/// Contains item-level information:
/// - Product reference and basic info
/// - Quantity
/// - Pricing
class CartItem {
  /// Unique cart item identifier
  final String cartItemId;

  /// Cart ID this item belongs to
  final String cartId;

  /// Product ID
  final String productId;

  /// Quantity ordered
  final double quantity;

  /// Unit price at time of adding to cart
  final double unitPrice;

  /// Subtotal (quantity * unitPrice)
  final double subtotal;

  /// Timestamp when item was added
  final DateTime addedAt;

  /// Product name from cart API
  final String? itemName;

  /// Product image URL from cart API
  final String? image;

  /// Conversion factor (m² to tiles) from cart API
  final double? conversionOfSm;

  const CartItem({
    required this.cartItemId,
    required this.cartId,
    required this.productId,
    required this.quantity,
    required this.unitPrice,
    required this.subtotal,
    required this.addedAt,
    this.itemName,
    this.image,
    this.conversionOfSm,
  });

  /// Calculate subtotal (for verification)
  double get calculatedSubtotal => quantity * unitPrice;

  /// Copy with method for immutability
  CartItem copyWith({
    String? cartItemId,
    String? cartId,
    String? productId,
    double? quantity,
    double? unitPrice,
    double? subtotal,
    DateTime? addedAt,
    String? itemName,
    String? image,
    double? conversionOfSm,
  }) {
    return CartItem(
      cartItemId: cartItemId ?? this.cartItemId,
      cartId: cartId ?? this.cartId,
      productId: productId ?? this.productId,
      quantity: quantity ?? this.quantity,
      unitPrice: unitPrice ?? this.unitPrice,
      subtotal: subtotal ?? this.subtotal,
      addedAt: addedAt ?? this.addedAt,
      itemName: itemName ?? this.itemName,
      image: image ?? this.image,
      conversionOfSm: conversionOfSm ?? this.conversionOfSm,
    );
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is CartItem &&
        other.cartItemId == cartItemId &&
        other.cartId == cartId &&
        other.productId == productId &&
        other.quantity == quantity &&
        other.unitPrice == unitPrice &&
        other.subtotal == subtotal;
  }

  @override
  int get hashCode {
    return Object.hash(
      cartItemId,
      cartId,
      productId,
      quantity,
      unitPrice,
      subtotal,
    );
  }

  @override
  String toString() {
    return 'CartItem(cartItemId: $cartItemId, productId: $productId, '
        'quantity: $quantity, unitPrice: $unitPrice, subtotal: $subtotal)';
  }
}
