import 'package:hive/hive.dart';

part 'amicale_model.g.dart';

@HiveType(typeId: 11)
class AmicaleModel 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(27)
  final String? stripeLocationId;

  @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 String? logoBase64; // Logo en base64 (data:image/png;base64,...)

  @HiveField(25)
  final bool chkUserDeletePass;

  @HiveField(26)
  final bool chkLotActif;

  AmicaleModel({
    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.stripeLocationId,
    this.chkDemo = false,
    this.chkCopieMailRecu = false,
    this.chkAcceptSms = false,
    this.chkActive = true,
    this.chkStripe = false,
    this.createdAt,
    this.updatedAt,
    this.chkMdpManuel = false,
    this.chkUsernameManuel = false,
    this.logoBase64,
    this.chkUserDeletePass = false,
    this.chkLotActif = false,
  });

  // Factory pour convertir depuis JSON (API)
  factory AmicaleModel.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
    final dynamic rawFkRegion = json['fk_region'];
    final int? fkRegion = rawFkRegion != null
        ? (rawFkRegion is String ? int.parse(rawFkRegion) : rawFkRegion as int)
        : null;

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

    // Convertir les booléens
    final bool chkDemo = json['chk_demo'] == 1 || json['chk_demo'] == true;
    final bool chkCopieMailRecu =
        json['chk_copie_mail_recu'] == 1 || json['chk_copie_mail_recu'] == true;
    final bool chkAcceptSms =
        json['chk_accept_sms'] == 1 || json['chk_accept_sms'] == true;
    final bool chkActive =
        json['chk_active'] == 1 || json['chk_active'] == true;
    final bool chkStripe =
        json['chk_stripe'] == 1 || json['chk_stripe'] == true;
    final bool chkMdpManuel =
        json['chk_mdp_manuel'] == 1 || json['chk_mdp_manuel'] == true;
    final bool chkUsernameManuel =
        json['chk_username_manuel'] == 1 || json['chk_username_manuel'] == true;
    final bool chkUserDeletePass =
        json['chk_user_delete_pass'] == 1 || json['chk_user_delete_pass'] == true;
    final bool chkLotActif =
        json['chk_lot_actif'] == 1 || json['chk_lot_actif'] == true;

    // Traiter le logo si présent
    String? logoBase64;
    if (json['logo'] != null && json['logo'] is Map) {
      final logoData = json['logo'] as Map<String, dynamic>;
      logoBase64 = logoData['data_url'] as String?;
    }

    // Traiter les dates si présentes
    DateTime? createdAt;
    if (json['created_at'] != null && json['created_at'] != '') {
      try {
        createdAt = DateTime.parse(json['created_at']);
      } catch (e) {
        createdAt = null;
      }
    }

    DateTime? updatedAt;
    if (json['updated_at'] != null && json['updated_at'] != '') {
      try {
        updatedAt = DateTime.parse(json['updated_at']);
      } catch (e) {
        updatedAt = null;
      }
    }

    return AmicaleModel(
      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'] ?? '',
      stripeLocationId: json['stripe_location_id'],
      chkDemo: chkDemo,
      chkCopieMailRecu: chkCopieMailRecu,
      chkAcceptSms: chkAcceptSms,
      chkActive: chkActive,
      chkStripe: chkStripe,
      createdAt: createdAt,
      updatedAt: updatedAt,
      chkMdpManuel: chkMdpManuel,
      chkUsernameManuel: chkUsernameManuel,
      logoBase64: logoBase64,
      chkUserDeletePass: chkUserDeletePass,
      chkLotActif: chkLotActif,
    );
  }

  // 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,
      'stripe_location_id': stripeLocationId,
      'chk_demo': chkDemo ? 1 : 0,
      'chk_copie_mail_recu': chkCopieMailRecu ? 1 : 0,
      'chk_accept_sms': chkAcceptSms ? 1 : 0,
      'chk_active': chkActive ? 1 : 0,
      'chk_stripe': chkStripe ? 1 : 0,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
      'chk_mdp_manuel': chkMdpManuel ? 1 : 0,
      'chk_username_manuel': chkUsernameManuel ? 1 : 0,
      'chk_user_delete_pass': chkUserDeletePass ? 1 : 0,
      'chk_lot_actif': chkLotActif ? 1 : 0,
      // Note: logoBase64 n'est pas envoyé via toJson (lecture seule depuis l'API)
    };
  }

  // Copier avec de nouvelles valeurs
  AmicaleModel 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,
    String? stripeLocationId,
    bool? chkDemo,
    bool? chkCopieMailRecu,
    bool? chkAcceptSms,
    bool? chkActive,
    bool? chkStripe,
    DateTime? createdAt,
    DateTime? updatedAt,
    bool? chkMdpManuel,
    bool? chkUsernameManuel,
    String? logoBase64,
    bool? chkUserDeletePass,
    bool? chkLotActif,
  }) {
    return AmicaleModel(
      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,
      stripeLocationId: stripeLocationId ?? this.stripeLocationId,
      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,
      logoBase64: logoBase64 ?? this.logoBase64,
      chkUserDeletePass: chkUserDeletePass ?? this.chkUserDeletePass,
      chkLotActif: chkLotActif ?? this.chkLotActif,
    );
  }
}
