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

part 'quote_item_model.g.dart';

@HiveType(typeId: HiveTypeIds.quoteItemModel)
class QuoteItemModel extends HiveObject {
  QuoteItemModel({
    required this.quoteItemId,
    required this.quoteId,
    required this.productId,
    required this.quantity,
    required this.originalPrice,
    required this.negotiatedPrice,
    required this.discountPercent,
    required this.subtotal,
    this.notes,
  });

  @HiveField(0)
  final String quoteItemId;
  @HiveField(1)
  final String quoteId;
  @HiveField(2)
  final String productId;
  @HiveField(3)
  final double quantity;
  @HiveField(4)
  final double originalPrice;
  @HiveField(5)
  final double negotiatedPrice;
  @HiveField(6)
  final double discountPercent;
  @HiveField(7)
  final double subtotal;
  @HiveField(8)
  final String? notes;

  factory QuoteItemModel.fromJson(Map<String, dynamic> json) => QuoteItemModel(
    quoteItemId: json['quote_item_id'] as String,
    quoteId: json['quote_id'] as String,
    productId: json['product_id'] as String,
    quantity: (json['quantity'] as num).toDouble(),
    originalPrice: (json['original_price'] as num).toDouble(),
    negotiatedPrice: (json['negotiated_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() => {
    'quote_item_id': quoteItemId,
    'quote_id': quoteId,
    'product_id': productId,
    'quantity': quantity,
    'original_price': originalPrice,
    'negotiated_price': negotiatedPrice,
    'discount_percent': discountPercent,
    'subtotal': subtotal,
    'notes': notes,
  };

  double get totalDiscount => originalPrice * quantity - subtotal;
}
