using Domain.Entities.Notes;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.Persistence.Configurations.Notes;

public class NoteConfiguration : IEntityTypeConfiguration<Note>
{
    public void Configure(EntityTypeBuilder<Note> builder)
    {
        builder.ToTable(nameof(Note), x => x.HasComment("쪽지"));
        builder.HasKey(x => x.ID);

        builder.HasOne(x => x.Sender).WithMany().HasForeignKey(x => x.SenderMemberID).OnDelete(DeleteBehavior.NoAction);
        builder.HasOne(x => x.Receiver).WithMany().HasForeignKey(x => x.ReceiverMemberID).OnDelete(DeleteBehavior.NoAction);

        builder.HasIndex(x => new { x.ReceiverMemberID, x.IsDeletedByReceiver, x.IsRead, x.CreatedAt }).IsDescending(false, false, false, true);
        builder.HasIndex(x => new { x.SenderMemberID, x.IsDeletedBySender, x.CreatedAt }).IsDescending(false, false, true);

        builder.Property(x => x.Title).HasMaxLength(200).IsRequired();
        builder.Property(x => x.Content).HasMaxLength(2000).IsRequired();
        builder.Property(x => x.RelatedType).HasMaxLength(50);
    }
}