import 'package:equatable/equatable.dart'; import 'package:flux/core/utils/extensions.dart'; import 'package:flux/features/operations/models/energy_operation_model.dart'; import 'package:flux/features/operations/models/entertainment_operation_model.dart'; import 'package:flux/features/operations/models/fin_operation_model.dart'; import 'package:flux/features/operations/models/operation_file_model.dart'; // <-- Aggiunto Import class OperationModel extends Equatable { final String? id; final DateTime? createdAt; final String storeId; final String? employeeId; final String? customerId; final String number; final bool isBozza; final String note; final bool resultOk; final String? customerDisplayName; final String companyId; // Telefonia final int al; final int mnp; final int nip; final int unica; final int telepass; // Moduli (Liste) final List energyOperations; final List finOperations; final List entertainmentOperations; // ALLEGATI (Aggiunto) final List files; const OperationModel({ this.id, this.createdAt, required this.storeId, this.employeeId, this.customerId, required this.number, this.isBozza = true, this.note = '', this.resultOk = true, this.al = 0, this.mnp = 0, this.nip = 0, this.unica = 0, this.telepass = 0, this.energyOperations = const [], this.finOperations = const [], this.entertainmentOperations = const [], this.files = const [], // <-- Aggiunto default vuoto this.customerDisplayName, required this.companyId, }); OperationModel copyWith({ String? id, DateTime? createdAt, String? storeId, String? employeeId, String? customerId, String? number, bool? isBozza, String? note, bool? resultOk, int? al, int? mnp, int? nip, int? unica, int? telepass, List? energyOperations, List? finOperations, List? entertainmentOperations, List? files, // <-- Aggiunto String? customerDisplayName, String? companyId, }) { return OperationModel( id: id ?? this.id, createdAt: createdAt ?? this.createdAt, storeId: storeId ?? this.storeId, employeeId: employeeId ?? this.employeeId, customerId: customerId ?? this.customerId, number: number ?? this.number, isBozza: isBozza ?? this.isBozza, note: note ?? this.note, resultOk: resultOk ?? this.resultOk, al: al ?? this.al, mnp: mnp ?? this.mnp, nip: nip ?? this.nip, unica: unica ?? this.unica, telepass: telepass ?? this.telepass, energyOperations: energyOperations ?? this.energyOperations, finOperations: finOperations ?? this.finOperations, entertainmentOperations: entertainmentOperations ?? this.entertainmentOperations, files: files ?? this.files, // <-- Aggiunto customerDisplayName: customerDisplayName ?? this.customerDisplayName, companyId: companyId ?? this.companyId, ); } @override List get props => [ id, createdAt, storeId, employeeId, customerId, number, isBozza, note, resultOk, al, mnp, nip, unica, telepass, energyOperations, finOperations, entertainmentOperations, files, // <-- Aggiunto customerDisplayName, companyId, ]; factory OperationModel.fromMap(Map map) { return OperationModel( id: map['id'].toString(), createdAt: map['created_at'] != null ? DateTime.parse(map['created_at']) : DateTime.now(), storeId: map['store_id'] ?? '', employeeId: map['employee_id']?.toString(), customerId: map['customer_id']?.toString(), number: map['number']?.toString() ?? '', isBozza: map['bozza'] ?? true, note: map['note'] ?? '', resultOk: map['result_ok'] ?? true, al: map['al'] ?? 0, mnp: map['mnp'] ?? 0, nip: map['nip'] ?? 0, unica: map['unica'] ?? 0, telepass: map['telepass'] ?? 0, // Estrazione sicura liste collegate energyOperations: (map['energy_operation'] as List?) ?.map((x) => EnergyOperationModel.fromMap(x)) .toList() ?? const [], finOperations: (map['fin_operation'] as List?) ?.map((x) => FinOperationModel.fromMap(x)) .toList() ?? const [], entertainmentOperations: (map['entertainment_operation'] as List?) ?.map((x) => EntertainmentOperationModel.fromMap(x)) .toList() ?? const [], // I FILE! (Assicurati che la foreign key su Supabase usi esattamente questo nome) files: (map['operation_file'] as List?) ?.map((x) => OperationFileModel.fromMap(x)) .toList() ?? const [], // Display name del cliente con fallback customerDisplayName: map['customer'] != null ? "${map['customer']['nome'] ?? ''}".myFormat() : "Cliente non assegnato", companyId: map['company_id'] as String, ); } Map toMap() { return { if (id != null) 'id': id, 'store_id': storeId, 'employee_id': employeeId, 'customer_id': customerId, 'number': number, 'bozza': isBozza, 'note': note, 'result_ok': resultOk, 'al': al, 'mnp': mnp, 'nip': nip, 'unica': unica, 'telepass': telepass, 'company_id': companyId, // Le liste non le mettiamo qui perché vanno in tabelle diverse! }; } }