import 'package:equatable/equatable.dart'; import 'package:flux/features/master_data/store/models/store_model.dart'; class ProviderModel extends Equatable { final String? id; final String name; final bool landline; final bool mobile; final bool energy; final bool insurance; final bool entertainment; final bool financing; final bool telepass; final bool other; final bool isActive; final String companyId; final List associatedStores; const ProviderModel({ this.id, required this.name, required this.landline, required this.mobile, required this.energy, required this.insurance, required this.entertainment, required this.financing, required this.telepass, required this.other, required this.isActive, required this.companyId, this.associatedStores = const [], }); factory ProviderModel.fromMap(Map map) { // Estraiamo la lista dalla pivot e poi prendiamo l'oggetto 'store' annidato final pivotList = map['associated_stores'] as List?; List stores = []; if (pivotList != null) { stores = pivotList .where((item) => item['store'] != null) // Sicurezza .map( (item) => StoreModel.fromMap(item['store'] as Map), ) .toList(); } return ProviderModel( id: map['id'], name: map['name'], landline: map['landline'] ?? false, mobile: map['mobile'] ?? false, energy: map['energy'] ?? false, insurance: map['insurance'] ?? false, entertainment: map['entertainment'] ?? false, financing: map['financing'] ?? false, telepass: map['telepass'] ?? false, other: map['other'] ?? false, isActive: map['is_active'] ?? true, companyId: map['company_id'], associatedStores: stores, ); } Map toMap() { final map = { 'name': name, 'landline': landline, 'mobile': mobile, 'energy': energy, 'insurance': insurance, 'entertainment': entertainment, 'financing': financing, 'telepass': telepass, 'other': other, 'is_active': isActive, 'company_id': companyId, }; // AGGIUNGIAMO L'ID SOLO SE NON È NULLO // Senza questo, l'upsert non sa dove andare a parare if (id != null) { map['id'] = id!; } return map; } @override List get props => [ id, name, landline, mobile, energy, insurance, entertainment, financing, telepass, other, isActive, companyId, associatedStores, ]; ProviderModel copyWith({ String? id, String? name, bool? landline, bool? mobile, bool? energy, bool? insurance, bool? entertainment, bool? financing, bool? telepass, bool? other, bool? isActive, String? companyId, List? associatedStores, }) { return ProviderModel( id: id ?? this.id, name: name ?? this.name, landline: landline ?? this.landline, mobile: mobile ?? this.mobile, energy: energy ?? this.energy, insurance: insurance ?? this.insurance, entertainment: entertainment ?? this.entertainment, financing: financing ?? this.financing, telepass: telepass ?? this.telepass, other: other ?? this.other, isActive: isActive ?? this.isActive, companyId: companyId ?? this.companyId, associatedStores: associatedStores ?? this.associatedStores, ); } }