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

part 'order_item_model.g.dart';

@HiveType(typeId: HiveTypeIds.orderItemModel)
class OrderItemModel extends HiveObject {
  OrderItemModel({
    required this.orderItemId,
    required this.orderId,
    required this.productId,
    required this.quantity,
    required this.unitPrice,
    required this.discountPercent,
    required this.subtotal,
    this.notes,
  });

  @HiveField(0)
  final String orderItemId;
  @HiveField(1)
  final String orderId;
  @HiveField(2)
  final String productId;
  @HiveField(3)
  final double quantity;
  @HiveField(4)
  final double unitPrice;
  @HiveField(5)
  final double discountPercent;
  @HiveField(6)
  final double subtotal;
  @HiveField(7)
  final String? notes;

  factory OrderItemModel.fromJson(Map<String, dynamic> json) => OrderItemModel(
    orderItemId: json['order_item_id'] as String,
    orderId: json['order_id'] as String,
    productId: json['product_id'] as String,
    quantity: (json['quantity'] as num).toDouble(),
    unitPrice: (json['unit_price'] as num).toDouble(),
    discountPercent: (json['discount_percent'] as num).toDouble(),
    subtotal: (json['subtotal'] as num).toDouble(),
    notes: json['notes'] as String?,
  );

  Map<String, dynamic> toJson() => {
    'order_item_id': orderItemId,
    'order_id': orderId,
    'product_id': productId,
    'quantity': quantity,
    'unit_price': unitPrice,
    'discount_percent': discountPercent,
    'subtotal': subtotal,
    'notes': notes,
  };
}
