/// Business Unit Data Model
///
/// Hive model for local storage of business units.
library;

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

part 'business_unit_model.g.dart';

/// Business Unit Model for Hive storage
@HiveType(typeId: HiveTypeIds.businessUnitModel)
class BusinessUnitModel extends HiveObject {
  /// Unique business unit identifier
  @HiveField(0)
  String id;

  /// Business unit code (e.g., "VIKD", "HSKD", "LPKD")
  @HiveField(1)
  String code;

  /// Display name
  @HiveField(2)
  String name;

  /// Description
  @HiveField(3)
  String? description;

  /// Whether this is the default unit
  @HiveField(4)
  bool isDefault;

  BusinessUnitModel({
    required this.id,
    required this.code,
    required this.name,
    this.description,
    this.isDefault = false,
  });

  /// Convert to domain entity
  BusinessUnit toEntity() {
    return BusinessUnit(
      id: id,
      code: code,
      name: name,
      description: description,
      isDefault: isDefault,
    );
  }

  /// Create from domain entity
  factory BusinessUnitModel.fromEntity(BusinessUnit entity) {
    return BusinessUnitModel(
      id: entity.id,
      code: entity.code,
      name: entity.name,
      description: entity.description,
      isDefault: entity.isDefault,
    );
  }

  /// Create from JSON
  factory BusinessUnitModel.fromJson(Map<String, dynamic> json) {
    return BusinessUnitModel(
      id: json['id'] as String,
      code: json['code'] as String,
      name: json['name'] as String,
      description: json['description'] as String?,
      isDefault: json['is_default'] as bool? ?? false,
    );
  }

  /// Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'code': code,
      'name': name,
      'description': description,
      'is_default': isDefault,
    };
  }

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