import '../../models/user_profile.dart'; import 'app_database.dart'; import 'user_profile_mapper.dart'; /// Drift-backed local profile storage (mobile and desktop native). class UserProfileLocalStore { UserProfileLocalStore(this._db); final AppDatabase _db; Stream watchProfile(String firebaseUid) { return _db.watchProfile(firebaseUid).map((UserProfileRow? row) { if (row == null) { return null; } return profileFromRow(row); }); } Future getProfile(String firebaseUid) async { final UserProfileRow? row = await _db.getProfile(firebaseUid); if (row == null) { return null; } return profileFromRow(row); } Future saveProfile(UserProfile profile, {required bool dirty}) async { await _db.upsertProfile( companionFromProfile(profile.copyWith(dirty: dirty)), ); if (dirty) { await _db.enqueueOutbox( SyncOutboxRowsCompanion.insert( firebaseUid: profile.firebaseUid, payloadJson: encodeProfilePayload(profile), createdAt: DateTime.now().toUtc(), ), ); } } Future> pendingOutboxProfiles(String firebaseUid) async { final List rows = await _db.pendingOutbox(firebaseUid); return rows .map((SyncOutboxRow row) => decodeProfilePayload(row.payloadJson)) .toList(); } Future clearOutbox(String firebaseUid) async { await _db.clearOutbox(firebaseUid); } Future deleteOutboxEntries(String firebaseUid) async { final List rows = await _db.pendingOutbox(firebaseUid); for (final SyncOutboxRow row in rows) { await _db.deleteOutboxEntry(row.id); } } Future clearLocal(String firebaseUid) async { await _db.clearOutbox(firebaseUid); await _db.deleteProfile(firebaseUid); } Future clearAll() async { await _db.clearAllProfiles(); await _db.clearAllOutbox(); } }