using Baya.Domain.Entities.Catalog;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Baya.Infrastructure.Persistence.Configuration.CatalogConfig;

internal sealed class ServiceOptionGroupConfig : IEntityTypeConfiguration<ServiceOptionGroup>
{
    public void Configure(EntityTypeBuilder<ServiceOptionGroup> builder)
    {
        builder.ToTable("ServiceOptionGroups", "catalog");

        builder.Property(g => g.NameFa).HasMaxLength(150).IsRequired();
        builder.Property(g => g.NameEn).HasMaxLength(150).IsRequired();
        builder.Property(g => g.IsRequired).HasDefaultValue(false);
        builder.Property(g => g.SortOrder).HasDefaultValue(0);
        builder.Property(g => g.IsActive).HasDefaultValue(true);

        // (service_category_id, sort_order) for the applicable-groups read. The nullable FK is deliberate —
        // a NULL category is the cross-category case and must not be broken by a required relationship.
        builder.HasIndex(g => new { g.ServiceCategoryId, g.SortOrder });

        builder.HasOne(g => g.ServiceCategory)
            .WithMany(c => c.OptionGroups)
            .HasForeignKey(g => g.ServiceCategoryId)
            .IsRequired(false);

        builder.HasQueryFilter(g => g.DeletedAt == null);
    }
}
