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

part 'cart_model.g.dart';

/// Cart Model - Type ID: 4
@HiveType(typeId: HiveTypeIds.cartModel)
class CartModel extends HiveObject {
  CartModel({
    required this.cartId,
    required this.userId,
    required this.totalAmount,
    required this.isSynced,
    required this.lastModified,
    required this.createdAt,
  });

  @HiveField(0)
  final String cartId;

  @HiveField(1)
  final String userId;

  @HiveField(2)
  final double totalAmount;

  @HiveField(3)
  final bool isSynced;

  @HiveField(4)
  final DateTime lastModified;

  @HiveField(5)
  final DateTime createdAt;

  factory CartModel.fromJson(Map<String, dynamic> json) {
    return CartModel(
      cartId: json['cart_id'] as String,
      userId: json['user_id'] as String,
      totalAmount: (json['total_amount'] as num).toDouble(),
      isSynced: json['is_synced'] as bool? ?? false,
      lastModified: DateTime.parse(json['last_modified'] as String),
      createdAt: DateTime.parse(json['created_at'] as String),
    );
  }

  Map<String, dynamic> toJson() => {
    'cart_id': cartId,
    'user_id': userId,
    'total_amount': totalAmount,
    'is_synced': isSynced,
    'last_modified': lastModified.toIso8601String(),
    'created_at': createdAt.toIso8601String(),
  };

  CartModel copyWith({
    String? cartId,
    String? userId,
    double? totalAmount,
    bool? isSynced,
    DateTime? lastModified,
    DateTime? createdAt,
  }) => CartModel(
    cartId: cartId ?? this.cartId,
    userId: userId ?? this.userId,
    totalAmount: totalAmount ?? this.totalAmount,
    isSynced: isSynced ?? this.isSynced,
    lastModified: lastModified ?? this.lastModified,
    createdAt: createdAt ?? this.createdAt,
  );
}
