using CMSMicroservice.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace CMSMicroservice.Infrastructure.Persistence.Configurations;
//کاربر
public class UserConfiguration : IEntityTypeConfiguration<User>
{
    public void Configure(EntityTypeBuilder<User> 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.FirstName).IsRequired(false);
        builder.Property(entity => entity.LastName).IsRequired(false);
        builder.Property(entity => entity.Mobile).IsRequired(true);
        builder.Property(entity => entity.NationalCode).IsRequired(false);
        builder.Property(entity => entity.AvatarPath).IsRequired(false);
        builder.Property(entity => entity.ReferralCode).IsRequired(true);
        builder.Property(entity => entity.IsMobileVerified ).IsRequired(true);
        builder.Property(entity => entity.MobileVerifiedAt).IsRequired(false);
        builder.Property(entity => entity.IsRulesAccepted).IsRequired(true);
        builder.Property(entity => entity.RulesAcceptedAt).IsRequired(false);
        builder.Property(entity => entity.EmailNotifications).IsRequired(true);
        builder.Property(entity => entity.SmsNotifications).IsRequired(true);
        builder.Property(entity => entity.PushNotifications).IsRequired(true);
        builder.Property(entity => entity.BirthDate).IsRequired(false);
        builder.Property(entity => entity.HashPassword).IsRequired(false);
        
        // ============= Network Club System Fields =============
        
        builder.Property(entity => entity.NetworkParentId).IsRequired(false);
        builder.Property(entity => entity.LegPosition).IsRequired(false);
        
        // رابطه با والد در شبکه باینری
        builder
            .HasOne(entity => entity.NetworkParent)
            .WithMany(entity => entity.NetworkChildren)
            .HasForeignKey(entity => entity.NetworkParentId)
            .IsRequired(false)
            .OnDelete(DeleteBehavior.Restrict);
        
        // Index برای NetworkParentId
        builder.HasIndex(e => e.NetworkParentId)
            .HasDatabaseName("IX_User_NetworkParentId");
        
        // Index برای LegPosition
        builder.HasIndex(e => e.LegPosition)
            .HasDatabaseName("IX_User_LegPosition");

    }
}
