using SharepointToolbox.Web.Core.Models; using SharepointToolbox.Web.Infrastructure.Persistence; namespace SharepointToolbox.Web.Services.Session; public class UserSessionService : IUserSessionService { private readonly ISessionManager _sessionManager; private readonly SettingsRepository _settingsRepo; private TenantProfile? _currentProfile; private AppSettings _settings = new(); public TenantProfile? CurrentProfile => _currentProfile; public bool HasProfile => _currentProfile is not null; public AppSettings Settings => _settings; public ReportBranding CurrentBranding => new(_settings.MspLogo, _currentProfile?.ClientLogo); public event Action? ProfileChanged; public UserSessionService(ISessionManager sessionManager, SettingsRepository settingsRepo) { _sessionManager = sessionManager; _settingsRepo = settingsRepo; _ = LoadSettingsAsync(); } public void SetProfile(TenantProfile profile) { _currentProfile = profile; ProfileChanged?.Invoke(); } public async Task ClearSessionAsync() { if (_currentProfile is not null) await _sessionManager.ClearSessionAsync(_currentProfile.TenantUrl); _currentProfile = null; ProfileChanged?.Invoke(); } public void UpdateSettings(AppSettings settings) { _settings = settings; _ = _settingsRepo.SaveAsync(settings); } private async Task LoadSettingsAsync() { try { _settings = await _settingsRepo.LoadAsync(); } catch { /* use defaults */ } } }