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

namespace CartWise.Infrastructure.Data.Configurations;

public class PurchaseItemConfiguration : IEntityTypeConfiguration<PurchaseItem>
{
    public void Configure(EntityTypeBuilder<PurchaseItem> builder)
    {
        builder.HasKey(i => i.PurchaseItemId);

        builder.Property(i => i.Description)
            .HasMaxLength(240)
            .IsRequired();

        builder.Property(i => i.Unit).HasMaxLength(32);

        builder.HasOne<Purchase>()
            .WithMany(p => p.Items)
            .HasForeignKey(i => i.PurchaseId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasOne<Product>()
            .WithMany()
            .HasForeignKey(i => i.ProductId)
            .OnDelete(DeleteBehavior.SetNull);

        builder.HasOne<GroceryConcept>()
            .WithMany()
            .HasForeignKey(i => i.GroceryConceptId)
            .OnDelete(DeleteBehavior.SetNull);

        builder.HasOne<ShoppingListItem>()
            .WithMany()
            .HasForeignKey(i => i.ShoppingListItemId)
            .OnDelete(DeleteBehavior.SetNull);

        builder.HasIndex(i => i.ProductId);
    }
}
