import 'package:hive/hive.dart';

part 'region_model.g.dart';

@HiveType(typeId: 7)
class RegionModel extends HiveObject {
  @HiveField(0)
  final int id;

  @HiveField(1)
  final int fkPays;

  @HiveField(2)
  final String libelle;

  @HiveField(3)
  final String? libelleLong;

  @HiveField(4)
  final String? tableOsm;

  @HiveField(5)
  final String? departements;

  @HiveField(6)
  final bool chkActive;

  RegionModel({
    required this.id,
    required this.fkPays,
    required this.libelle,
    this.libelleLong,
    this.tableOsm,
    this.departements,
    this.chkActive = true,
  });

  // Constructeur de copie
  RegionModel copyWith({
    int? id,
    int? fkPays,
    String? libelle,
    String? libelleLong,
    String? tableOsm,
    String? departements,
    bool? chkActive,
  }) {
    return RegionModel(
      id: id ?? this.id,
      fkPays: fkPays ?? this.fkPays,
      libelle: libelle ?? this.libelle,
      libelleLong: libelleLong ?? this.libelleLong,
      tableOsm: tableOsm ?? this.tableOsm,
      departements: departements ?? this.departements,
      chkActive: chkActive ?? this.chkActive,
    );
  }

  // Conversion depuis JSON
  factory RegionModel.fromJson(Map<String, dynamic> json) {
    return RegionModel(
      id: json['id'] as int,
      fkPays: json['fk_pays'] as int,
      libelle: json['libelle'] as String,
      libelleLong: json['libelle_long'] as String?,
      tableOsm: json['table_osm'] as String?,
      departements: json['departements'] as String?,
      chkActive: json['chk_active'] == 1 || json['chk_active'] == true,
    );
  }

  // Conversion vers JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'fk_pays': fkPays,
      'libelle': libelle,
      'libelle_long': libelleLong,
      'table_osm': tableOsm,
      'departements': departements,
      'chk_active': chkActive ? 1 : 0,
    };
  }

  @override
  String toString() {
    return 'RegionModel(id: $id, libelle: $libelle)';
  }
}
