/// Data Model: Category
///
/// Data Transfer Object for category information.
library;

import 'package:hive_ce/hive.dart';
import 'package:worker/core/constants/storage_constants.dart';
import 'package:worker/features/products/domain/entities/category.dart';

part 'category_model.g.dart';

/// Category Model
///
/// Used for:
/// - JSON serialization/deserialization
/// - Hive local database storage
/// - Converting to/from domain entity
///
/// Hive Type ID: 27 (from HiveTypeIds.categoryModel)
@HiveType(typeId: HiveTypeIds.categoryModel)
class CategoryModel extends HiveObject {
  /// Unique identifier
  @HiveField(0)
  final String id;

  /// Category name
  @HiveField(1)
  final String name;

  /// Category description
  @HiveField(2)
  final String description;

  /// Icon name or emoji
  @HiveField(3)
  final String icon;

  /// Display order
  @HiveField(4)
  final int order;

  CategoryModel({
    required this.id,
    required this.name,
    required this.description,
    required this.icon,
    required this.order,
  });

  /// From JSON constructor
  factory CategoryModel.fromJson(Map<String, dynamic> json) {
    return CategoryModel(
      id: json['id'] as String,
      name: json['name'] as String,
      description: json['description'] as String,
      icon: json['icon'] as String,
      order: json['order'] as int,
    );
  }

  /// To JSON method
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'icon': icon,
      'order': order,
    };
  }

  /// Convert to domain entity
  Category toEntity() {
    return Category(
      id: id,
      name: name,
      description: description,
      icon: icon,
      order: order,
    );
  }

  /// Create from domain entity
  factory CategoryModel.fromEntity(Category entity) {
    return CategoryModel(
      id: entity.id,
      name: entity.name,
      description: entity.description,
      icon: entity.icon,
      order: entity.order,
    );
  }

  /// Copy with method
  CategoryModel copyWith({
    String? id,
    String? name,
    String? description,
    String? icon,
    int? order,
  }) {
    return CategoryModel(
      id: id ?? this.id,
      name: name ?? this.name,
      description: description ?? this.description,
      icon: icon ?? this.icon,
      order: order ?? this.order,
    );
  }

  @override
  String toString() {
    return 'CategoryModel(id: $id, name: $name, order: $order)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is CategoryModel &&
        other.id == id &&
        other.name == name &&
        other.description == description &&
        other.icon == icon &&
        other.order == order;
  }

  @override
  int get hashCode {
    return Object.hash(id, name, description, icon, order);
  }
}
