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

namespace CartWise.Infrastructure.Data.Configurations;

public class ShoppingListConfiguration : IEntityTypeConfiguration<ShoppingList>
{
    public void Configure(EntityTypeBuilder<ShoppingList> builder)
    {
        builder.HasKey(l => l.ShoppingListId);

        builder.Property(l => l.Name)
            .HasMaxLength(160)
            .IsRequired();

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

        builder.HasOne<ApplicationUser>()
            .WithMany()
            .HasForeignKey(l => l.CreatedByUserId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasIndex(l => new { l.HouseholdId, l.Status });

        builder.Metadata.FindNavigation(nameof(ShoppingList.Items))!
            .SetPropertyAccessMode(PropertyAccessMode.Field);
    }
}
