/// City Model
///
/// Hive model for caching city/province data.
library;

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

part 'city_model.g.dart';

/// City Model
///
/// Hive model for storing city/province data with offline support.
@HiveType(typeId: HiveTypeIds.cityModel)
class CityModel extends HiveObject {
  /// Frappe ERPNext name/ID
  @HiveField(0)
  String name;

  /// Display name (city_name)
  @HiveField(1)
  String cityName;

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

  CityModel({
    required this.name,
    required this.cityName,
    required this.code,
  });

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

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

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

  /// Create from domain entity
  factory CityModel.fromEntity(City entity) {
    return CityModel(
      name: entity.name,
      cityName: entity.cityName,
      code: entity.code,
    );
  }

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