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

namespace CartWise.Infrastructure.Data.Configurations;

public class ShoppingListItemConfiguration : IEntityTypeConfiguration<ShoppingListItem>
{
    public void Configure(EntityTypeBuilder<ShoppingListItem> builder)
    {
        builder.HasKey(i => i.ShoppingListItemId);

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

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

        builder.Property(i => i.Notes)
            .HasMaxLength(500);

        builder.Property(i => i.RowVersion)
            .IsConcurrencyToken();

        builder.HasOne<ShoppingList>()
            .WithMany(l => l.Items)
            .HasForeignKey(i => i.ShoppingListId)
            .OnDelete(DeleteBehavior.Cascade);

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

        builder.HasOne<ApplicationUser>()
            .WithMany()
            .HasForeignKey(i => i.AddedByUserId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasIndex(i => new { i.ShoppingListId, i.Status });
    }
}
