using Microsoft.EntityFrameworkCore; using line_gestao_api.Models; namespace line_gestao_api.Data; public class AppDbContext : DbContext { public AppDbContext(DbContextOptions options) : base(options) { } public DbSet Users => Set(); // ✅ tabela para espelhar a planilha (GERAL) public DbSet MobileLines => Set(); // ✅ tabela para espelhar a aba MUREG public DbSet MuregLines => Set(); // ✅ tabela para espelhar o FATURAMENTO (PF/PJ) public DbSet BillingClients => Set(); // ✅ tabela DADOS DOS USUÁRIOS public DbSet UserDatas => Set(); // ✅ tabela VIGÊNCIA public DbSet VigenciaLines => Set(); // ✅ tabela TROCA DE NÚMERO public DbSet TrocaNumeroLines => Set(); // ✅ tabela NOTIFICAÇÕES public DbSet Notifications => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // ========================= // ✅ USER // ========================= modelBuilder.Entity() .HasIndex(u => u.Email) .IsUnique(); // ========================= // ✅ GERAL (MobileLine) // ========================= modelBuilder.Entity(e => { // Mantém UNIQUE por Linha (se Linha puder ser null no banco, Postgres aceita múltiplos nulls) e.HasIndex(x => x.Linha).IsUnique(); // performance e.HasIndex(x => x.Chip); e.HasIndex(x => x.Cliente); e.HasIndex(x => x.Usuario); e.HasIndex(x => x.Skil); e.HasIndex(x => x.Status); }); // ========================= // ✅ MUREG (FK para MobileLines) // ========================= modelBuilder.Entity(e => { e.HasIndex(x => x.Item); e.HasIndex(x => x.ICCID); e.HasIndex(x => x.LinhaAntiga); e.HasIndex(x => x.LinhaNova); // FK + index e.HasIndex(x => x.MobileLineId); e.HasOne(x => x.MobileLine) .WithMany(m => m.Muregs) .HasForeignKey(x => x.MobileLineId) .OnDelete(DeleteBehavior.Restrict); }); // ========================= // ✅ FATURAMENTO (BillingClient) // ========================= modelBuilder.Entity(e => { // ⚠️ só mantenha se seu banco realmente usa esse nome e.ToTable("billing_clients"); e.HasKey(x => x.Id); e.Property(x => x.Tipo).HasMaxLength(2); e.Property(x => x.Cliente).HasMaxLength(255); e.HasIndex(x => x.Tipo); e.HasIndex(x => x.Cliente); e.HasIndex(x => new { x.Tipo, x.Cliente }); e.HasIndex(x => x.Item); }); // ========================= // ✅ DADOS DOS USUÁRIOS (UserData) // ✅ (SEM "Nome" pq não existe no model) // ========================= modelBuilder.Entity(e => { e.HasIndex(x => x.Item); e.HasIndex(x => x.Cliente); e.HasIndex(x => x.Linha); e.HasIndex(x => x.Cpf); e.HasIndex(x => x.Email); }); // ========================= // ✅ VIGÊNCIA // ========================= modelBuilder.Entity(e => { e.HasIndex(x => x.Item); e.HasIndex(x => x.Cliente); e.HasIndex(x => x.Linha); e.HasIndex(x => x.DtTerminoFidelizacao); }); // ========================= // ✅ TROCA NÚMERO // ========================= modelBuilder.Entity(e => { e.HasIndex(x => x.Item); e.HasIndex(x => x.LinhaAntiga); e.HasIndex(x => x.LinhaNova); e.HasIndex(x => x.ICCID); e.HasIndex(x => x.DataTroca); }); // ========================= // ✅ NOTIFICAÇÕES // ========================= modelBuilder.Entity(e => { e.HasIndex(x => x.DedupKey).IsUnique(); e.HasIndex(x => x.UserId); e.HasIndex(x => x.Cliente); e.HasIndex(x => x.Lida); e.HasIndex(x => x.Data); e.HasIndex(x => x.VigenciaLineId); e.HasOne(x => x.User) .WithMany() .HasForeignKey(x => x.UserId) .OnDelete(DeleteBehavior.Restrict); e.HasOne(x => x.VigenciaLine) .WithMany() .HasForeignKey(x => x.VigenciaLineId) .OnDelete(DeleteBehavior.Restrict); }); } }