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 createAccount(Map accountData) async { await _firestore.collection('accounts').add(accountData); } Future updateAccount(String accountId, Map accountData) async { await _firestore.collection('accounts').doc(accountId).update(accountData); } Future deleteAccount(String accountId) async { await _firestore.collection('accounts').doc(accountId).delete(); } }