import 'package:hive/hive.dart';

part 'client_model.g.dart';

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

  @HiveField(1)
  final String name;

  @HiveField(2)
  final String? adresse1;

  @HiveField(3)
  final String? adresse2;

  @HiveField(4)
  final String? codePostal;

  @HiveField(5)
  final String? ville;

  @HiveField(6)
  final int? fkRegion;

  @HiveField(7)
  final String? libRegion;

  @HiveField(8)
  final int? fkType;

  @HiveField(9)
  final String? phone;

  @HiveField(10)
  final String? mobile;

  @HiveField(11)
  final String? email;

  @HiveField(12)
  final String? gpsLat;

  @HiveField(13)
  final String? gpsLng;

  @HiveField(14)
  final String? stripeId;

  @HiveField(15)
  final bool? chkDemo;

  @HiveField(16)
  final bool? chkCopieMailRecu;

  @HiveField(17)
  final bool? chkAcceptSms;

  @HiveField(18)
  final bool? chkActive;

  @HiveField(19)
  final bool? chkStripe;

  @HiveField(20)
  final DateTime? createdAt;

  @HiveField(21)
  final DateTime? updatedAt;

  @HiveField(22)
  final bool? chkMdpManuel;

  @HiveField(23)
  final bool? chkUsernameManuel;

  @HiveField(24)
  final bool? chkUserDeletePass;

  ClientModel({
    required this.id,
    required this.name,
    this.adresse1,
    this.adresse2,
    this.codePostal,
    this.ville,
    this.fkRegion,
    this.libRegion,
    this.fkType,
    this.phone,
    this.mobile,
    this.email,
    this.gpsLat,
    this.gpsLng,
    this.stripeId,
    this.chkDemo,
    this.chkCopieMailRecu,
    this.chkAcceptSms,
    this.chkActive,
    this.chkStripe,
    this.createdAt,
    this.updatedAt,
    this.chkMdpManuel,
    this.chkUsernameManuel,
    this.chkUserDeletePass,
  });

  // Factory pour convertir depuis JSON (API)
  factory ClientModel.fromJson(Map<String, dynamic> json) {
    // Convertir l'ID en int, qu'il soit déjà int ou string
    final dynamic rawId = json['id'];
    final int id = rawId is String ? int.parse(rawId) : rawId as int;

    // Convertir fk_region en int si présent
    int? fkRegion;
    if (json['fk_region'] != null) {
      final dynamic rawFkRegion = json['fk_region'];
      fkRegion = rawFkRegion is String ? int.parse(rawFkRegion) : rawFkRegion as int;
    }

    // Convertir fk_type en int si présent
    int? fkType;
    if (json['fk_type'] != null) {
      final dynamic rawFkType = json['fk_type'];
      fkType = rawFkType is String ? int.parse(rawFkType) : rawFkType as int;
    }

    return ClientModel(
      id: id,
      name: json['name'] ?? '',
      adresse1: json['adresse1'],
      adresse2: json['adresse2'],
      codePostal: json['code_postal'],
      ville: json['ville'],
      fkRegion: fkRegion,
      libRegion: json['lib_region'],
      fkType: fkType,
      phone: json['phone'],
      mobile: json['mobile'],
      email: json['email'],
      gpsLat: json['gps_lat'],
      gpsLng: json['gps_lng'],
      stripeId: json['stripe_id'],
      chkDemo: json['chk_demo'] == 1 || json['chk_demo'] == true,
      chkCopieMailRecu: json['chk_copie_mail_recu'] == 1 || json['chk_copie_mail_recu'] == true,
      chkAcceptSms: json['chk_accept_sms'] == 1 || json['chk_accept_sms'] == true,
      chkActive: json['chk_active'] == 1 || json['chk_active'] == true,
      chkStripe: json['chk_stripe'] == 1 || json['chk_stripe'] == true,
      createdAt: json['created_at'] != null ? DateTime.parse(json['created_at']) : null,
      updatedAt: json['updated_at'] != null ? DateTime.parse(json['updated_at']) : null,
      chkMdpManuel: json['chk_mdp_manuel'] == 1 || json['chk_mdp_manuel'] == true,
      chkUsernameManuel: json['chk_username_manuel'] == 1 || json['chk_username_manuel'] == true,
      chkUserDeletePass: json['chk_user_delete_pass'] == 1 || json['chk_user_delete_pass'] == true,
    );
  }

  // Convertir en JSON pour l'API
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'adresse1': adresse1,
      'adresse2': adresse2,
      'code_postal': codePostal,
      'ville': ville,
      'fk_region': fkRegion,
      'lib_region': libRegion,
      'fk_type': fkType,
      'phone': phone,
      'mobile': mobile,
      'email': email,
      'gps_lat': gpsLat,
      'gps_lng': gpsLng,
      'stripe_id': stripeId,
      'chk_demo': chkDemo,
      'chk_copie_mail_recu': chkCopieMailRecu,
      'chk_accept_sms': chkAcceptSms,
      'chk_active': chkActive,
      'chk_stripe': chkStripe,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
      'chk_mdp_manuel': chkMdpManuel,
      'chk_username_manuel': chkUsernameManuel,
      'chk_user_delete_pass': chkUserDeletePass,
    };
  }

  // Copier avec de nouvelles valeurs
  ClientModel copyWith({
    String? name,
    String? adresse1,
    String? adresse2,
    String? codePostal,
    String? ville,
    int? fkRegion,
    String? libRegion,
    int? fkType,
    String? phone,
    String? mobile,
    String? email,
    String? gpsLat,
    String? gpsLng,
    String? stripeId,
    bool? chkDemo,
    bool? chkCopieMailRecu,
    bool? chkAcceptSms,
    bool? chkActive,
    bool? chkStripe,
    DateTime? createdAt,
    DateTime? updatedAt,
    bool? chkMdpManuel,
    bool? chkUsernameManuel,
    bool? chkUserDeletePass,
  }) {
    return ClientModel(
      id: id,
      name: name ?? this.name,
      adresse1: adresse1 ?? this.adresse1,
      adresse2: adresse2 ?? this.adresse2,
      codePostal: codePostal ?? this.codePostal,
      ville: ville ?? this.ville,
      fkRegion: fkRegion ?? this.fkRegion,
      libRegion: libRegion ?? this.libRegion,
      fkType: fkType ?? this.fkType,
      phone: phone ?? this.phone,
      mobile: mobile ?? this.mobile,
      email: email ?? this.email,
      gpsLat: gpsLat ?? this.gpsLat,
      gpsLng: gpsLng ?? this.gpsLng,
      stripeId: stripeId ?? this.stripeId,
      chkDemo: chkDemo ?? this.chkDemo,
      chkCopieMailRecu: chkCopieMailRecu ?? this.chkCopieMailRecu,
      chkAcceptSms: chkAcceptSms ?? this.chkAcceptSms,
      chkActive: chkActive ?? this.chkActive,
      chkStripe: chkStripe ?? this.chkStripe,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      chkMdpManuel: chkMdpManuel ?? this.chkMdpManuel,
      chkUsernameManuel: chkUsernameManuel ?? this.chkUsernameManuel,
      chkUserDeletePass: chkUserDeletePass ?? this.chkUserDeletePass,
    );
  }
}
