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 DiscountOrderConfiguration : IEntityTypeConfiguration<DiscountOrder>
{
    public void Configure(EntityTypeBuilder<DiscountOrder> 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.TotalAmount).IsRequired();
        builder.Property(entity => entity.DiscountBalanceUsed).IsRequired();
        builder.Property(entity => entity.GatewayAmountPaid).IsRequired();
        builder.Property(entity => entity.VatAmount).IsRequired();
        builder.Property(entity => entity.PaymentStatus).IsRequired();
        builder.Property(entity => entity.UserAddressId).IsRequired();
        builder.Property(entity => entity.DeliveryStatus).IsRequired();
        
        builder.Property(entity => entity.TrackingCode)
            .HasMaxLength(100);
        
        builder.Property(entity => entity.DeliveryDescription)
            .HasMaxLength(500);
        
        // Relationship: User -> Orders
        builder
            .HasOne(entity => entity.User)
            .WithMany(entity => entity.DiscountOrders)
            .HasForeignKey(entity => entity.UserId)
            .OnDelete(DeleteBehavior.Restrict);
        
        // Relationship: Transaction (nullable)
        builder
            .HasOne(entity => entity.Transaction)
            .WithMany()
            .HasForeignKey(entity => entity.TransactionId)
            .OnDelete(DeleteBehavior.Restrict);
        
        // Relationship: UserAddress
        builder
            .HasOne(entity => entity.UserAddress)
            .WithMany()
            .HasForeignKey(entity => entity.UserAddressId)
            .OnDelete(DeleteBehavior.Restrict);
    }
}
