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

namespace CMSMicroservice.Infrastructure.Persistence.Configurations;

/// <summary>
/// پرداخت کمیسیون به کاربران
/// </summary>
public class UserCommissionPayoutConfiguration : IEntityTypeConfiguration<UserCommissionPayout>
{
    public void Configure(EntityTypeBuilder<UserCommissionPayout> 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.WeekDefinitionId).IsRequired();
        builder.Property(entity => entity.WeeklyPoolId).IsRequired();
        builder.Property(entity => entity.PackageId).IsRequired();
        builder.Property(entity => entity.BalancesEarned).IsRequired();
        builder.Property(entity => entity.ValuePerBalance).IsRequired();
        builder.Property(entity => entity.TotalAmount).IsRequired();
        builder.Property(entity => entity.Status).IsRequired();
        builder.Property(entity => entity.PaidAt).IsRequired(false);
        builder.Property(entity => entity.WithdrawalMethod).IsRequired(false);
        builder.Property(entity => entity.IbanNumber).IsRequired(false).HasMaxLength(26);
        builder.Property(entity => entity.WithdrawnAt).IsRequired(false);
        builder.Property(entity => entity.ProcessedBy).IsRequired(false).HasMaxLength(200);
        builder.Property(entity => entity.ProcessedAt).IsRequired(false);
        builder.Property(entity => entity.RejectionReason).IsRequired(false).HasMaxLength(500);
        
        // رابطه با User
        builder.HasOne(entity => entity.User)
            .WithMany(u => u.CommissionPayouts)
            .HasForeignKey(entity => entity.UserId)
            .OnDelete(DeleteBehavior.Restrict);
        
        // رابطه با WeeklyCommissionPool
        builder.HasOne(entity => entity.WeeklyPool)
            .WithMany(wp => wp.UserCommissionPayouts)
            .HasForeignKey(entity => entity.WeeklyPoolId)
            .OnDelete(DeleteBehavior.Restrict);
        
        // رابطه با WeekDefinition (Required FK)
        builder.HasOne(entity => entity.WeekDefinition)
            .WithMany(wd => wd.UserCommissionPayouts)
            .HasForeignKey(entity => entity.WeekDefinitionId)
            .IsRequired()
            .OnDelete(DeleteBehavior.Restrict);
        
        // رابطه با Package
        builder.HasOne(entity => entity.Package)
            .WithMany()
            .HasForeignKey(entity => entity.PackageId)
            .OnDelete(DeleteBehavior.Restrict);
        
        // Composite Index برای UserId، WeekDefinitionId و PackageId
        builder.HasIndex(e => new { e.UserId, e.WeekDefinitionId, e.PackageId })
            .IsUnique()
            .HasDatabaseName("IX_UserCommissionPayout_User_WeekDef_Package");
        
        // Index برای WeeklyPoolId
        builder.HasIndex(e => e.WeeklyPoolId)
            .HasDatabaseName("IX_UserCommissionPayout_WeeklyPoolId");
        
        // Index برای Status
        builder.HasIndex(e => e.Status)
            .HasDatabaseName("IX_UserCommissionPayout_Status");
        
    }
}
