using CMSMicroservice.Domain.Entities.DiscountShop;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace CMSMicroservice.Infrastructure.Persistence.Configurations.DiscountShop;

/// <summary>
/// تنظیمات EF Core برای جزئیات سفارش فروشگاه تخفیفی
/// </summary>
public class DiscountOrderDetailConfiguration : IEntityTypeConfiguration<DiscountOrderDetail>
{
    public void Configure(EntityTypeBuilder<DiscountOrderDetail> 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.DiscountOrderId).IsRequired();
        builder.Property(entity => entity.ProductId).IsRequired();
        builder.Property(entity => entity.Count).IsRequired();
        builder.Property(entity => entity.UnitPrice).IsRequired();
        builder.Property(entity => entity.DiscountPercentUsed).IsRequired();
        builder.Property(entity => entity.DiscountAmount).IsRequired();
        builder.Property(entity => entity.FinalPrice).IsRequired();
        
        // Relationship: Order -> OrderDetails
        builder
            .HasOne(entity => entity.DiscountOrder)
            .WithMany(entity => entity.OrderDetails)
            .HasForeignKey(entity => entity.DiscountOrderId)
            .OnDelete(DeleteBehavior.Cascade);
        
        // Relationship: Product
        builder
            .HasOne(entity => entity.Product)
            .WithMany(entity => entity.OrderDetails)
            .HasForeignKey(entity => entity.ProductId)
            .OnDelete(DeleteBehavior.Restrict);
    }
}
