import 'package:hive_ce/hive.dart';
import 'package:worker/core/constants/storage_constants.dart';

part 'cart_item_model.g.dart';

/// Cart Item Model - Type ID: 5
@HiveType(typeId: HiveTypeIds.cartItemModel)
class CartItemModel extends HiveObject {
  CartItemModel({
    required this.cartItemId,
    required this.cartId,
    required this.productId,
    required this.quantity,
    required this.unitPrice,
    required this.subtotal,
    required this.addedAt,
  });

  @HiveField(0)
  final String cartItemId;

  @HiveField(1)
  final String cartId;

  @HiveField(2)
  final String productId;

  @HiveField(3)
  final double quantity;

  @HiveField(4)
  final double unitPrice;

  @HiveField(5)
  final double subtotal;

  @HiveField(6)
  final DateTime addedAt;

  factory CartItemModel.fromJson(Map<String, dynamic> json) {
    return CartItemModel(
      cartItemId: json['cart_item_id'] as String,
      cartId: json['cart_id'] as String,
      productId: json['product_id'] as String,
      quantity: (json['quantity'] as num).toDouble(),
      unitPrice: (json['unit_price'] as num).toDouble(),
      subtotal: (json['subtotal'] as num).toDouble(),
      addedAt: DateTime.parse(json['added_at'] as String),
    );
  }

  Map<String, dynamic> toJson() => {
    'cart_item_id': cartItemId,
    'cart_id': cartId,
    'product_id': productId,
    'quantity': quantity,
    'unit_price': unitPrice,
    'subtotal': subtotal,
    'added_at': addedAt.toIso8601String(),
  };

  CartItemModel copyWith({
    String? cartItemId,
    String? cartId,
    String? productId,
    double? quantity,
    double? unitPrice,
    double? subtotal,
    DateTime? addedAt,
  }) => CartItemModel(
    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,
  );
}
