using MongoDB.Driver; using BCards.Web.Models; using BCards.Web.Repositories; namespace BCards.Tests.Fixtures; public class DatabaseFixture : IDisposable { public IMongoDatabase Database { get; } public IUserRepository UserRepository { get; } public IUserPageRepository UserPageRepository { get; } public ICategoryRepository CategoryRepository { get; } public DatabaseFixture(IMongoDatabase database) { Database = database; UserRepository = new UserRepository(database); UserPageRepository = new UserPageRepository(database); CategoryRepository = new CategoryRepository(database); InitializeTestData().Wait(); } private async Task InitializeTestData() { // Clear any existing data await Database.DropCollectionAsync("users"); await Database.DropCollectionAsync("userpages"); await Database.DropCollectionAsync("categories"); // Initialize test categories var categories = new List { new Category { Id = "tech", Name = "Tecnologia", Description = "Tecnologia e inovação" }, new Category { Id = "business", Name = "Negócios", Description = "Empresas e negócios" }, new Category { Id = "personal", Name = "Pessoal", Description = "Páginas pessoais" } }; await CategoryRepository.CreateManyAsync(categories); } public async Task CreateTestUser(PlanType planType = PlanType.Trial, string? email = null) { var user = new User { Id = Guid.NewGuid().ToString(), Email = email ?? $"test-{Guid.NewGuid():N}@example.com", Name = "Test User", CurrentPlan = planType.ToString(), CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow, IsActive = true }; await UserRepository.CreateAsync(user); return user; } public async Task CreateTestUserPage(string userId, string category = "tech", int linkCount = 1, int productLinkCount = 0) { var userPage = new UserPage { Id = Guid.NewGuid().ToString(), UserId = userId, DisplayName = "Test Page", Category = category, Slug = $"test-page-{Guid.NewGuid():N}", Bio = "Test page description", Status = BCards.Web.ViewModels.PageStatus.Active, Links = new List(), CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }; // Add normal links for (int i = 0; i < linkCount; i++) { userPage.Links.Add(new LinkItem { Title = $"Test Link {i + 1}", Url = $"https://example.com/link{i + 1}", Description = $"Test link {i + 1} description", Icon = "fas fa-link", IsActive = true, Order = i, Type = LinkType.Normal }); } // Add product links for (int i = 0; i < productLinkCount; i++) { userPage.Links.Add(new LinkItem { Title = $"Test Product {i + 1}", Url = $"https://example.com/product{i + 1}", Description = $"Test product {i + 1} description", Icon = "fas fa-shopping-cart", IsActive = true, Order = linkCount + i, Type = LinkType.Product, ProductTitle = $"Product {i + 1}", ProductPrice = "R$ 99,90", ProductDescription = $"Amazing product {i + 1}" }); } await UserPageRepository.CreateAsync(userPage); return userPage; } public async Task CleanDatabase() { var collections = new[] { "users", "userpages", "categories", "livepages" }; foreach (var collection in collections) { try { await Database.DropCollectionAsync(collection); } catch (Exception) { // Ignore errors when collection doesn't exist } } await InitializeTestData(); } public void Dispose() { try { Database.Client.DropDatabase(Database.DatabaseNamespace.DatabaseName); } catch (Exception) { // Ignore cleanup errors } } }