/// Ward Model
///
/// Hive model for caching ward/district data.
library;

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

part 'ward_model.g.dart';

/// Ward Model
///
/// Hive model for storing ward/district data with offline support.
@HiveType(typeId: HiveTypeIds.wardModel)
class WardModel extends HiveObject {
  /// Frappe ERPNext name/ID
  @HiveField(0)
  String name;

  /// Display name (ward_name)
  @HiveField(1)
  String wardName;

  /// Ward code
  @HiveField(2)
  String code;

  WardModel({
    required this.name,
    required this.wardName,
    required this.code,
  });

  /// Create from JSON (API response)
  factory WardModel.fromJson(Map<String, dynamic> json) {
    return WardModel(
      name: json['name'] as String? ?? '',
      wardName: json['ward_name'] as String? ?? '',
      code: json['code'] as String? ?? '',
    );
  }

  /// Convert to JSON (API request)
  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'ward_name': wardName,
      'code': code,
    };
  }

  /// Convert to domain entity
  Ward toEntity() {
    return Ward(
      name: name,
      wardName: wardName,
      code: code,
    );
  }

  /// Create from domain entity
  factory WardModel.fromEntity(Ward entity) {
    return WardModel(
      name: entity.name,
      wardName: entity.wardName,
      code: entity.code,
    );
  }

  @override
  String toString() => 'WardModel(name: $name, wardName: $wardName, code: $code)';
}
