import { Injectable, inject } from '@angular/core'; import { PlatformService } from '../../core/platform'; import { clearStoredLocalAppData } from '../../core/storage/current-user-storage'; import { DatabaseService } from './database.service'; import { UserLogoutService } from '../../domains/authentication/application/services/user-logout.service'; import { AuthTokenStoreService } from '../../domains/authentication/application/services/auth-token-store.service'; import { loadCapacitorAttachmentFilesystem } from '../../domains/attachment/infrastructure/services/capacitor-attachment-filesystem.adapter'; const CAPACITOR_APP_DATA_ROOT = 'metoyou'; export interface EraseLocalUserDataResult { restartRequired: boolean; } @Injectable({ providedIn: 'root' }) export class LocalUserDataService { private readonly platform = inject(PlatformService); private readonly database = inject(DatabaseService); private readonly userLogout = inject(UserLogoutService); private readonly authTokenStore = inject(AuthTokenStoreService); async eraseLocalUserData(): Promise { if (!this.platform.isCapacitor) { throw new Error('Local user data erase is only supported on native mobile shells.'); } await this.database.clearAllData(); await this.deleteCapacitorAttachmentTree(); this.authTokenStore.clearAllTokens(); clearStoredLocalAppData(); this.userLogout.logout(); return { restartRequired: false }; } private async deleteCapacitorAttachmentTree(): Promise { const filesystem = await loadCapacitorAttachmentFilesystem(); if (!filesystem) { return; } try { await filesystem.filesystem.rmdir({ path: CAPACITOR_APP_DATA_ROOT, directory: filesystem.directory, recursive: true }); } catch { // Missing directory is fine during erase. } } }