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

part 'redeemed_gift_model.g.dart';

@HiveType(typeId: HiveTypeIds.redeemedGiftModel)
class RedeemedGiftModel extends HiveObject {
  RedeemedGiftModel({
    required this.giftId,
    required this.userId,
    required this.catalogId,
    required this.name,
    required this.description,
    this.voucherCode,
    this.qrCodeImage,
    required this.giftType,
    required this.pointsCost,
    required this.cashValue,
    this.expiryDate,
    required this.status,
    required this.redeemedAt,
    this.usedAt,
    this.usedLocation,
    this.usedReference,
  });

  @HiveField(0)
  final String giftId;
  @HiveField(1)
  final String userId;
  @HiveField(2)
  final String catalogId;
  @HiveField(3)
  final String name;
  @HiveField(4)
  final String description;
  @HiveField(5)
  final String? voucherCode;
  @HiveField(6)
  final String? qrCodeImage;
  @HiveField(7)
  final GiftCategory giftType;
  @HiveField(8)
  final int pointsCost;
  @HiveField(9)
  final double cashValue;
  @HiveField(10)
  final DateTime? expiryDate;
  @HiveField(11)
  final GiftStatus status;
  @HiveField(12)
  final DateTime redeemedAt;
  @HiveField(13)
  final DateTime? usedAt;
  @HiveField(14)
  final String? usedLocation;
  @HiveField(15)
  final String? usedReference;

  factory RedeemedGiftModel.fromJson(Map<String, dynamic> json) =>
      RedeemedGiftModel(
        giftId: json['gift_id'] as String,
        userId: json['user_id'] as String,
        catalogId: json['catalog_id'] as String,
        name: json['name'] as String,
        description: json['description'] as String,
        voucherCode: json['voucher_code'] as String?,
        qrCodeImage: json['qr_code_image'] as String?,
        giftType: GiftCategory.values.firstWhere(
          (e) => e.name == json['gift_type'],
        ),
        pointsCost: json['points_cost'] as int,
        cashValue: (json['cash_value'] as num).toDouble(),
        expiryDate: json['expiry_date'] != null
            ? DateTime.parse(json['expiry_date']?.toString() ?? '')
            : null,
        status: GiftStatus.values.firstWhere((e) => e.name == json['status']),
        redeemedAt: DateTime.parse(json['redeemed_at']?.toString() ?? ''),
        usedAt: json['used_at'] != null
            ? DateTime.parse(json['used_at']?.toString() ?? '')
            : null,
        usedLocation: json['used_location'] as String?,
        usedReference: json['used_reference'] as String?,
      );

  Map<String, dynamic> toJson() => {
    'gift_id': giftId,
    'user_id': userId,
    'catalog_id': catalogId,
    'name': name,
    'description': description,
    'voucher_code': voucherCode,
    'qr_code_image': qrCodeImage,
    'gift_type': giftType.name,
    'points_cost': pointsCost,
    'cash_value': cashValue,
    'expiry_date': expiryDate?.toIso8601String(),
    'status': status.name,
    'redeemed_at': redeemedAt.toIso8601String(),
    'used_at': usedAt?.toIso8601String(),
    'used_location': usedLocation,
    'used_reference': usedReference,
  };

  bool get isExpired =>
      expiryDate != null && DateTime.now().isAfter(expiryDate!);
  bool get isUsed => status == GiftStatus.used;
  bool get isActive => status == GiftStatus.active && !isExpired;
}
