import 'package:equatable/equatable.dart'; import 'package:flux/core/utils/string_extensions.dart'; import 'package:flux/features/customers/models/customer_file_model.dart'; class CustomerModel extends Equatable { final String? id; // Bigint in SQL final DateTime? createdAt; final String nome; final String telefono; final String email; final String note; final DateTime? dataUltimoContatto; final bool nonDisturbare; final String companyId; // UUID final bool isActive; final List files; const CustomerModel({ this.id, this.createdAt, required this.nome, required this.telefono, required this.email, required this.note, this.dataUltimoContatto, this.nonDisturbare = false, required this.companyId, this.isActive = true, this.files = const [], }); @override List get props => [ id, createdAt, nome, telefono, email, note, dataUltimoContatto, nonDisturbare, companyId, isActive, files, ]; CustomerModel copyWith({ String? id, DateTime? createdAt, String? nome, String? telefono, String? email, String? note, DateTime? dataUltimoContatto, bool? nonDisturbare, String? companyId, bool? isActive, List? files, }) { return CustomerModel( id: id ?? this.id, createdAt: createdAt ?? this.createdAt, nome: nome ?? this.nome, telefono: telefono ?? this.telefono, email: email ?? this.email, note: note ?? this.note, dataUltimoContatto: dataUltimoContatto ?? this.dataUltimoContatto, nonDisturbare: nonDisturbare ?? this.nonDisturbare, companyId: companyId ?? this.companyId, isActive: isActive ?? this.isActive, files: files ?? this.files, ); } factory CustomerModel.fromMap(Map map) { return CustomerModel( id: map['id'] as String, createdAt: map['created_at'] != null ? DateTime.parse(map['created_at']) : null, nome: (map['nome'] as String).myFormat(), telefono: map['telefono'], email: map['email'], note: map['note'] ?? '', dataUltimoContatto: map['data_ultimo_contatto'] != null ? DateTime.parse(map['data_ultimo_contatto']) : null, nonDisturbare: map['non_disturbare'] ?? false, companyId: map['company_id'] as String, isActive: map['is_active'] ?? true, files: (map['customer_file'] as List?) ?.map((x) => CustomerFileModel.fromMap(x)) .toList() ?? const [], ); } Map toJson() { return { if (id != null) 'id': id, 'nome': nome.toLowerCase().trim(), 'telefono': telefono, 'email': email.toLowerCase().trim(), 'note': note, if (dataUltimoContatto != null) 'data_ultimo_contatto': dataUltimoContatto!.toIso8601String(), 'non_disturbare': nonDisturbare, 'company_id': companyId, 'is_active': isActive, }; } }