using CartWise.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace CartWise.Infrastructure.Data.Configurations;

public class HouseholdProductPreferenceConfiguration : IEntityTypeConfiguration<HouseholdProductPreference>
{
    public void Configure(EntityTypeBuilder<HouseholdProductPreference> builder)
    {
        builder.HasKey(p => p.HouseholdProductPreferenceId);

        builder.Property(p => p.PreferredUnit)
            .HasMaxLength(32);

        builder.HasIndex(p => new { p.HouseholdId, p.GroceryConceptId })
            .IsUnique();

        builder.HasOne<Household>()
            .WithMany()
            .HasForeignKey(p => p.HouseholdId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasOne<GroceryConcept>()
            .WithMany()
            .HasForeignKey(p => p.GroceryConceptId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasOne<Product>()
            .WithMany()
            .HasForeignKey(p => p.PreferredProductId)
            .OnDelete(DeleteBehavior.SetNull);
    }
}
