using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Nuuru.Server.Models.Messaging; namespace Nuuru.Server.Data.Configurations { public class ConversationConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { builder.HasKey(c => c.Id); builder.HasIndex(c => c.LastMessageAt); builder.HasIndex(c => c.CreatorId); // Creator relationship builder.HasOne(c => c.Creator) .WithMany() .HasForeignKey(c => c.CreatorId) .OnDelete(DeleteBehavior.Restrict); // Messages cascade delete builder.HasMany(c => c.Messages) .WithOne(m => m.Conversation) .HasForeignKey(m => m.ConversationId) .OnDelete(DeleteBehavior.Cascade); // Participants cascade delete builder.HasMany(c => c.Participants) .WithOne(p => p.Conversation) .HasForeignKey(p => p.ConversationId) .OnDelete(DeleteBehavior.Cascade); } } public class ConversationParticipantConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { // Composite key builder.HasKey(cp => new { cp.ConversationId, cp.UserId }); builder.HasIndex(cp => cp.UserId); builder.HasIndex(cp => new { cp.UserId, cp.HasLeft }); // Cascade delete: when user is deleted, remove their participation records builder.HasOne(cp => cp.User) .WithMany(u => u.ConversationParticipations) .HasForeignKey(cp => cp.UserId) .OnDelete(DeleteBehavior.Cascade); } } public class MessageConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { builder.HasKey(m => m.Id); builder.HasIndex(m => m.ConversationId); builder.HasIndex(m => new { m.ConversationId, m.CreatedAt }); // Author relationship - link to ApplicationUser.Messages builder.HasOne(m => m.Author) .WithMany(u => u.Messages) .HasForeignKey(m => m.AuthorId) .OnDelete(DeleteBehavior.Restrict); // Mentions cascade delete builder.HasMany(m => m.Mentions) .WithOne(mm => mm.Message) .HasForeignKey(mm => mm.MessageId) .OnDelete(DeleteBehavior.Cascade); } } public class MessageMentionConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { // Composite key builder.HasKey(mm => new { mm.MessageId, mm.MentionedUserId }); // Cascade delete: when mentioned user is deleted, remove mention records builder.HasOne(mm => mm.MentionedUser) .WithMany() .HasForeignKey(mm => mm.MentionedUserId) .OnDelete(DeleteBehavior.Cascade); } } }