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

namespace Baya.Infrastructure.Persistence.Configuration.CatalogConfig;

internal sealed class NurseServiceVariantOptionConfig : IEntityTypeConfiguration<NurseServiceVariantOption>
{
    public void Configure(EntityTypeBuilder<NurseServiceVariantOption> builder)
    {
        builder.ToTable("NurseServiceVariantOptions", "catalog");

        // One value per dimension per variant. The unique index is the authoritative backstop; the handler
        // validates the same rule for a clean message. Its leading column is variant_id, so it also serves
        // "load a variant's full option set" — no separate variant_id index needed.
        builder.HasIndex(o => new { o.VariantId, o.OptionGroupId })
            .IsUnique()
            .HasDatabaseName("UX_NurseServiceVariantOptions_Variant_Group");

        builder.HasOne(o => o.Variant)
            .WithMany(v => v.Options)
            .HasForeignKey(o => o.VariantId)
            .IsRequired();

        builder.HasOne(o => o.OptionGroup)
            .WithMany()
            .HasForeignKey(o => o.OptionGroupId)
            .IsRequired();

        builder.HasOne(o => o.OptionValue)
            .WithMany()
            .HasForeignKey(o => o.OptionValueId)
            .IsRequired();
    }
}
