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 DiscountCategoryConfiguration : IEntityTypeConfiguration<DiscountCategory>
{
    public void Configure(EntityTypeBuilder<DiscountCategory> 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.Name)
            .IsRequired()
            .HasMaxLength(100);
        
        builder.Property(entity => entity.Title)
            .IsRequired()
            .HasMaxLength(200);
        
        builder.Property(entity => entity.Description)
            .HasMaxLength(1000);
        
        builder.Property(entity => entity.ImagePath);
        
        builder.Property(entity => entity.IsActive)
            .IsRequired()
            .HasDefaultValue(true);
        
        // Self-referencing relationship for parent/child categories
        builder
            .HasOne(entity => entity.ParentCategory)
            .WithMany(entity => entity.ChildCategories)
            .HasForeignKey(entity => entity.ParentCategoryId)
            .OnDelete(DeleteBehavior.Restrict);
    }
}
