using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace CMSMicroservice.Infrastructure.Persistence.Configurations;

/// <summary>
/// تاریخچه جابجایی در شبکه باینری
/// </summary>
public class NetworkMembershipHistoryConfiguration : IEntityTypeConfiguration<NetworkMembershipHistory>
{
    public void Configure(EntityTypeBuilder<NetworkMembershipHistory> builder)
    {
        builder.HasQueryFilter(p => !p.IsDeleted);
        builder.Ignore(entity => entity.DomainEvents);
        
        builder.HasKey(entity => entity.Id);
        builder.Property(entity => entity.Id).UseIdentityColumn();
        
        builder.Property(entity => entity.UserId).IsRequired();
        builder.Property(entity => entity.OldParentId).IsRequired(false);
        builder.Property(entity => entity.NewParentId).IsRequired(false);
        builder.Property(entity => entity.OldLegPosition).IsRequired(false);
        builder.Property(entity => entity.NewLegPosition).IsRequired(false);
        builder.Property(entity => entity.Action).IsRequired();
        builder.Property(entity => entity.Reason).IsRequired(false).HasMaxLength(500);
        builder.Property(entity => entity.PerformedBy).IsRequired(false).HasMaxLength(100);
        
        // Index برای UserId و Created
        builder.HasIndex(e => new { e.UserId, e.Created })
            .HasDatabaseName("IX_NetworkMembershipHistory_UserId_Created");
        
        // Index برای Action
        builder.HasIndex(e => e.Action)
            .HasDatabaseName("IX_NetworkMembershipHistory_Action");
    }
}
