using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using MotoresArgentinosV2.Infrastructure.Data; using MotoresArgentinosV2.Core.Entities; using MotoresArgentinosV2.Core.Interfaces; namespace MotoresArgentinosV2.API.Controllers; [ApiController] [Route("api/[controller]")] public class SeedController : ControllerBase { private readonly MotoresV2DbContext _context; private readonly IPasswordService _passwordService; public SeedController(MotoresV2DbContext context, IPasswordService passwordService) { _context = context; _passwordService = passwordService; } [HttpPost("database")] public async Task SeedDatabase() { // 1. Asegurar Marcas y Modelos if (!await _context.Brands.AnyAsync()) { var toyota = new Brand { VehicleTypeID = 1, Name = "Toyota" }; var ford = new Brand { VehicleTypeID = 1, Name = "Ford" }; var vw = new Brand { VehicleTypeID = 1, Name = "Volkswagen" }; var honda = new Brand { VehicleTypeID = 2, Name = "Honda" }; var yamaha = new Brand { VehicleTypeID = 2, Name = "Yamaha" }; _context.Brands.AddRange(toyota, ford, vw, honda, yamaha); await _context.SaveChangesAsync(); _context.Models.AddRange( new Model { BrandID = toyota.BrandID, Name = "Corolla" }, new Model { BrandID = toyota.BrandID, Name = "Hilux" }, new Model { BrandID = ford.BrandID, Name = "Ranger" }, new Model { BrandID = vw.BrandID, Name = "Amarok" }, new Model { BrandID = honda.BrandID, Name = "Wave 110" }, new Model { BrandID = yamaha.BrandID, Name = "FZ FI" } ); await _context.SaveChangesAsync(); } // 2. Crear Usuarios de Prueba var testUser = await _context.Users.FirstOrDefaultAsync(u => u.UserName == "testuser"); if (testUser == null) { testUser = new User { UserName = "testuser", Email = "test@motores.com.ar", PasswordHash = _passwordService.HashPassword("test123"), FirstName = "Usuario", LastName = "Prueba", MigrationStatus = 1, UserType = 1 }; _context.Users.Add(testUser); } var adminUser = await _context.Users.FirstOrDefaultAsync(u => u.UserName == "admin"); if (adminUser == null) { adminUser = new User { UserName = "admin", Email = "admin@motoresargentinos.com.ar", PasswordHash = _passwordService.HashPassword("admin123"), FirstName = "Admin", LastName = "Motores", MigrationStatus = 1, UserType = 3 // ADMIN }; _context.Users.Add(adminUser); } await _context.SaveChangesAsync(); // 3. Crear Avisos de Prueba if (!await _context.Ads.AnyAsync()) { var brands = await _context.Brands.ToListAsync(); var models = await _context.Models.ToListAsync(); var ad1 = new Ad { UserID = testUser.UserID, VehicleTypeID = 1, BrandID = brands.First(b => b.Name == "Toyota").BrandID, ModelID = models.First(m => m.Name == "Corolla").ModelID, VersionName = "Toyota Corolla 1.8 XLI", Year = 2022, KM = 15000, Price = 25000, Currency = "USD", Description = "Excelente estado, único dueño. Service al día.", StatusID = 4, // Activo IsFeatured = true, CreatedAt = DateTime.UtcNow, PublishedAt = DateTime.UtcNow }; var ad2 = new Ad { UserID = testUser.UserID, VehicleTypeID = 2, BrandID = brands.First(b => b.Name == "Honda").BrandID, ModelID = models.First(m => m.Name == "Wave 110").ModelID, VersionName = "Honda Wave 110 S", Year = 2023, KM = 2500, Price = 1800, Currency = "USD", Description = "Impecable, como nueva. Muy económica.", StatusID = 4, // Activo CreatedAt = DateTime.UtcNow, PublishedAt = DateTime.UtcNow }; var ad3 = new Ad { UserID = testUser.UserID, VehicleTypeID = 1, BrandID = brands.First(b => b.Name == "Ford").BrandID, ModelID = models.First(m => m.Name == "Ranger").ModelID, VersionName = "Ford Ranger Limited 4x4", Year = 2021, KM = 35000, Price = 42000, Currency = "USD", Description = "Camioneta impecable, lista para transferir.", StatusID = 3, // Moderacion Pendiente CreatedAt = DateTime.UtcNow }; _context.Ads.AddRange(ad1, ad2, ad3); await _context.SaveChangesAsync(); // Agregar fotos _context.AdPhotos.AddRange( new AdPhoto { AdID = ad1.AdID, FilePath = "https://images.unsplash.com/photo-1621007947382-bb3c3994e3fb?auto=format&fit=crop&q=80&w=1200", IsCover = true }, new AdPhoto { AdID = ad1.AdID, FilePath = "https://images.unsplash.com/photo-1590362891991-f776e933a68e?auto=format&fit=crop&q=80&w=1200" }, new AdPhoto { AdID = ad1.AdID, FilePath = "https://images.unsplash.com/photo-1549317661-bd32c8ce0db2?auto=format&fit=crop&q=80&w=1200" }, new AdPhoto { AdID = ad2.AdID, FilePath = "https://images.unsplash.com/photo-1558981403-c5f91cbba527?auto=format&fit=crop&q=80&w=800", IsCover = true }, new AdPhoto { AdID = ad3.AdID, FilePath = "https://images.unsplash.com/photo-1533473359331-0135ef1b58bf?auto=format&fit=crop&q=80&w=1200", IsCover = true } ); // Agregar Características Técnicas _context.AdFeatures.AddRange( new AdFeature { AdID = ad1.AdID, FeatureKey = "Combustible", FeatureValue = "Nafta" }, new AdFeature { AdID = ad1.AdID, FeatureKey = "Transmision", FeatureValue = "Automática" }, new AdFeature { AdID = ad1.AdID, FeatureKey = "Color", FeatureValue = "Blanco" }, new AdFeature { AdID = ad2.AdID, FeatureKey = "Combustible", FeatureValue = "Nafta" }, new AdFeature { AdID = ad2.AdID, FeatureKey = "Color", FeatureValue = "Rojo" } ); await _context.SaveChangesAsync(); } return Ok("Database seeded successfully with Features and Multiple Photos"); } [HttpPost("reset")] public async Task ResetDatabase() { _context.ChangeTracker.Clear(); await _context.Database.ExecuteSqlRawAsync("DELETE FROM Transactions"); await _context.Database.ExecuteSqlRawAsync("DELETE FROM AdPhotos"); await _context.Database.ExecuteSqlRawAsync("DELETE FROM AdFeatures"); await _context.Database.ExecuteSqlRawAsync("DELETE FROM Ads"); await _context.Database.ExecuteSqlRawAsync("DELETE FROM Models"); await _context.Database.ExecuteSqlRawAsync("DELETE FROM Brands"); return await SeedDatabase(); } }