using BCards.Web.Models; using BCards.Web.Repositories; using BCards.Web.ViewModels; namespace BCards.Web.Services; public class LivePageService : ILivePageService { private readonly ILivePageRepository _livePageRepository; private readonly IUserPageRepository _userPageRepository; private readonly ILogger _logger; public LivePageService( ILivePageRepository livePageRepository, IUserPageRepository userPageRepository, ILogger logger) { _livePageRepository = livePageRepository; _userPageRepository = userPageRepository; _logger = logger; } public async Task GetByCategoryAndSlugAsync(string category, string slug) { return await _livePageRepository.GetByCategoryAndSlugAsync(category, slug); } public async Task> GetAllActiveAsync() { return await _livePageRepository.GetAllActiveAsync(); } public async Task GetLivePageFromUserPageId(string userPageId) { return await _livePageRepository.GetByOriginalPageIdAsync(userPageId); } public async Task SyncFromUserPageAsync(string userPageId) { var userPage = await _userPageRepository.GetByIdAsync(userPageId); if (userPage == null) throw new InvalidOperationException($"UserPage {userPageId} not found"); if (userPage.Status != PageStatus.Active) throw new InvalidOperationException("UserPage must be Active to sync to LivePage"); // Verificar se já existe LivePage para este UserPage var existingLivePage = await _livePageRepository.GetByOriginalPageIdAsync(userPageId); var livePage = new LivePage { OriginalPageId = userPageId, UserId = userPage.UserId, Category = userPage.Category, Slug = userPage.Slug, DisplayName = userPage.DisplayName, Bio = userPage.Bio, ProfileImage = userPage.ProfileImage, BusinessType = userPage.BusinessType, Theme = userPage.Theme, Links = userPage.Links, SeoSettings = userPage.SeoSettings, Language = userPage.Language, Analytics = new LivePageAnalytics { TotalViews = existingLivePage?.Analytics?.TotalViews ?? 0, TotalClicks = existingLivePage?.Analytics?.TotalClicks ?? 0, LastViewedAt = existingLivePage?.Analytics?.LastViewedAt }, PublishedAt = userPage.ApprovedAt ?? DateTime.UtcNow }; if (existingLivePage != null) { // Atualizar existente livePage.Id = existingLivePage.Id; livePage.CreatedAt = existingLivePage.CreatedAt; _logger.LogInformation("Updating existing LivePage {LivePageId} from UserPage {UserPageId}", livePage.Id, userPageId); return await _livePageRepository.UpdateAsync(livePage); } else { // Criar nova _logger.LogInformation("Creating new LivePage from UserPage {UserPageId}", userPageId); return await _livePageRepository.CreateAsync(livePage); } } public async Task DeleteByOriginalPageIdAsync(string originalPageId) { _logger.LogInformation("Deleting LivePage for UserPage {UserPageId}", originalPageId); return await _livePageRepository.DeleteByOriginalPageIdAsync(originalPageId); } public async Task IncrementViewAsync(string livePageId) { try { await _livePageRepository.IncrementViewAsync(livePageId); } catch (Exception ex) { _logger.LogError(ex, "Failed to increment view for LivePage {LivePageId}", livePageId); } } public async Task IncrementLinkClickAsync(string livePageId, int linkIndex) { try { await _livePageRepository.IncrementLinkClickAsync(livePageId, linkIndex); } catch (Exception ex) { _logger.LogError(ex, "Failed to increment click for LivePage {LivePageId} link {LinkIndex}", livePageId, linkIndex); } } }