using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using MiningService.Domain.AggregatesModel.ReferralAggregate;

namespace MiningService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: EF Core configuration for Referral aggregate.
/// VI: Cấu hình EF Core cho Referral aggregate.
/// </summary>
public class ReferralEntityTypeConfiguration : IEntityTypeConfiguration<Referral>
{
    public void Configure(EntityTypeBuilder<Referral> builder)
    {
        builder.ToTable("Referrals");
        
        builder.HasKey(r => r.Id);
        
        builder.HasIndex(r => r.ReferrerId)
            .HasDatabaseName("IX_Referrals_ReferrerId");
        
        builder.HasIndex(r => r.ReferredId)
            .IsUnique()
            .HasDatabaseName("IX_Referrals_ReferredId");
        
        builder.Property(r => r.ReferralCode)
            .HasMaxLength(10);
        
        builder.Property(r => r.BonusRate)
            .HasPrecision(5, 4);
        
        builder.Ignore(r => r.DomainEvents);
    }
}
