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

namespace CMSMicroservice.Infrastructure.Persistence.Configurations;

/// <summary>
/// تاریخچه تغییرات عضویت باشگاه
/// </summary>
public class ClubMembershipHistoryConfiguration : IEntityTypeConfiguration<ClubMembershipHistory>
{
    public void Configure(EntityTypeBuilder<ClubMembershipHistory> 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.ClubMembershipId).IsRequired();
        builder.Property(entity => entity.UserId).IsRequired();
        builder.Property(entity => entity.OldIsActive).IsRequired();
        builder.Property(entity => entity.NewIsActive).IsRequired();
        builder.Property(entity => entity.OldInitialContribution).IsRequired(false);
        builder.Property(entity => entity.NewInitialContribution).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);
        
        // رابطه با ClubMembership
        builder.HasOne(entity => entity.ClubMembership)
            .WithMany(cm => cm.ClubMembershipHistories)
            .HasForeignKey(entity => entity.ClubMembershipId)
            .OnDelete(DeleteBehavior.Restrict);
        
        // Index برای UserId و Created (برای تاریخچه)
        builder.HasIndex(e => new { e.UserId, e.Created })
            .HasDatabaseName("IX_ClubMembershipHistory_UserId_Created");
        
        // Index برای ClubMembershipId
        builder.HasIndex(e => e.ClubMembershipId)
            .HasDatabaseName("IX_ClubMembershipHistory_ClubMembershipId");
        
        // Index برای Action
        builder.HasIndex(e => e.Action)
            .HasDatabaseName("IX_ClubMembershipHistory_Action");
    }
}
