import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:travel_mate/services/error_service.dart'; import '../data/models/group_member.dart'; import '../data/models/account.dart'; class AccountRepository { final FirebaseFirestore _firestore = FirebaseFirestore.instance; final _errorService = ErrorService(); CollectionReference get _accountCollection => _firestore.collection('accounts'); CollectionReference _membersCollection(String accountId) { return _accountCollection.doc(accountId).collection('members'); } Future createAccountWithMembers({ required Account account, required List members, }) async { try { return await _firestore.runTransaction((transaction) async { final accountRef = _accountCollection.doc(); final accountData = account.toMap(); transaction.set(accountRef, accountData); for (var member in members) { final memberRef = accountRef.collection('members').doc(member.userId); transaction.set(memberRef, member.toMap()); } return accountRef.id; }); } catch (e) { throw Exception('Erreur lors de la création du compte: $e'); } } Stream> getAccountByUserId(String userId) { return _accountCollection .snapshots() .asyncMap((snapshot) async { List userAccounts = []; for (var accountDoc in snapshot.docs) { try { final accountId = accountDoc.id; final memberDoc = await accountDoc.reference .collection('members') .doc(userId) .get(); if (memberDoc.exists) { final accountData = accountDoc.data() as Map; final account = Account.fromMap(accountData); final members = await getAccountMembers(accountId); userAccounts.add(account.copyWith(members: members)); } else { _errorService.logInfo('account_repository.dart', 'Utilisateur NON membre de $accountId'); } } catch (e, stackTrace) { _errorService.logError(e.toString(), stackTrace); } } return userAccounts; }) .distinct((prev, next) { if (prev.length != next.length) return false; final prevIds = prev.map((a) => a.id).toSet(); final nextIds = next.map((a) => a.id).toSet(); final identical = prevIds.difference(nextIds).isEmpty && nextIds.difference(prevIds).isEmpty; return identical; }) .handleError((error, stackTrace) { _errorService.logError(error, stackTrace); return []; }); } Future> getAccountMembers(String accountId) async { try { final snapshot = await _membersCollection(accountId).get(); return snapshot.docs .map((doc) { return GroupMember.fromMap( doc.data() as Map, doc.id, ); }) .toList(); } catch (e) { throw Exception('Erreur lors de la récupération des membres: $e'); } } Future getAccountById(String accountId) async { return await _firestore.collection('accounts').doc(accountId).get(); } Future updateAccount(String accountId, Account account) async { try { await _firestore.collection('accounts').doc(accountId).update(account.toMap()); } catch (e) { _errorService.logError('account_repository.dart', 'Erreur lors de la mise à jour du compte: $e'); } } Future deleteAccount(String tripId) async { try { final querySnapshot = await _firestore .collection('accounts') .where('tripId', isEqualTo: tripId) .get(); if (querySnapshot.docs.isEmpty) { throw Exception('Aucun compte trouvé pour ce voyage'); } final docId = querySnapshot.docs.first.id; final membersSnapshot = await _membersCollection(docId).get(); for (var memberDoc in membersSnapshot.docs) { await _membersCollection(docId).doc(memberDoc.id).delete(); } await _accountCollection.doc(docId).delete(); } catch (e) { _errorService.logError('account_repository.dart', 'Erreur lors de la suppression du compte: $e'); } } Stream> watchGroupMembers(String accountId) { return _membersCollection(accountId).snapshots().map( (snapshot) => snapshot.docs .map((doc) => GroupMember.fromMap( doc.data() as Map, doc.id, )) .toList(), ); } }