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

part 'gift_catalog_model.g.dart';

@HiveType(typeId: HiveTypeIds.giftCatalogModel)
class GiftCatalogModel extends HiveObject {
  GiftCatalogModel({
    required this.catalogId,
    required this.name,
    required this.description,
    this.imageUrl,
    required this.category,
    required this.pointsCost,
    required this.cashValue,
    required this.quantityAvailable,
    required this.quantityRedeemed,
    this.termsConditions,
    required this.isActive,
    this.validFrom,
    this.validUntil,
    required this.createdAt,
    this.updatedAt,
  });

  @HiveField(0)
  final String catalogId;
  @HiveField(1)
  final String name;
  @HiveField(2)
  final String description;
  @HiveField(3)
  final String? imageUrl;
  @HiveField(4)
  final GiftCategory category;
  @HiveField(5)
  final int pointsCost;
  @HiveField(6)
  final double cashValue;
  @HiveField(7)
  final int quantityAvailable;
  @HiveField(8)
  final int quantityRedeemed;
  @HiveField(9)
  final String? termsConditions;
  @HiveField(10)
  final bool isActive;
  @HiveField(11)
  final DateTime? validFrom;
  @HiveField(12)
  final DateTime? validUntil;
  @HiveField(13)
  final DateTime createdAt;
  @HiveField(14)
  final DateTime? updatedAt;

  factory GiftCatalogModel.fromJson(Map<String, dynamic> json) =>
      GiftCatalogModel(
        catalogId: json['catalog_id'] as String,
        name: json['name'] as String,
        description: json['description'] as String,
        imageUrl: json['image_url'] as String?,
        category: GiftCategory.values.firstWhere(
          (e) => e.name == json['category'],
        ),
        pointsCost: json['points_cost'] as int,
        cashValue: (json['cash_value'] as num).toDouble(),
        quantityAvailable: json['quantity_available'] as int,
        quantityRedeemed: json['quantity_redeemed'] as int? ?? 0,
        termsConditions: json['terms_conditions'] as String?,
        isActive: json['is_active'] as bool? ?? true,
        validFrom: json['valid_from'] != null
            ? DateTime.parse(json['valid_from']?.toString() ?? '')
            : null,
        validUntil: json['valid_until'] != null
            ? DateTime.parse(json['valid_until']?.toString() ?? '')
            : null,
        createdAt: DateTime.parse(json['created_at']?.toString() ?? ''),
        updatedAt: json['updated_at'] != null
            ? DateTime.parse(json['updated_at']?.toString() ?? '')
            : null,
      );

  Map<String, dynamic> toJson() => {
    'catalog_id': catalogId,
    'name': name,
    'description': description,
    'image_url': imageUrl,
    'category': category.name,
    'points_cost': pointsCost,
    'cash_value': cashValue,
    'quantity_available': quantityAvailable,
    'quantity_redeemed': quantityRedeemed,
    'terms_conditions': termsConditions,
    'is_active': isActive,
    'valid_from': validFrom?.toIso8601String(),
    'valid_until': validUntil?.toIso8601String(),
    'created_at': createdAt.toIso8601String(),
    'updated_at': updatedAt?.toIso8601String(),
  };

  bool get isAvailable => isActive && quantityAvailable > quantityRedeemed;
  bool get isValid {
    final now = DateTime.now();
    if (validFrom != null && now.isBefore(validFrom!)) return false;
    if (validUntil != null && now.isAfter(validUntil!)) return false;
    return true;
  }
}
