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

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class BinConfiguration : IEntityTypeConfiguration<Bin>
{
    public void Configure(EntityTypeBuilder<Bin> builder)
    {
        builder.ToTable("bins");
        builder.HasKey(b => b.BinId);

        builder.Property(b => b.Code).IsRequired().HasMaxLength(50);
        builder.Property(b => b.BinType).HasMaxLength(50);

        builder.HasOne(b => b.Warehouse)
            .WithMany(w => w.Bins)
            .HasForeignKey(b => b.WarehouseId)
            .OnDelete(DeleteBehavior.Cascade);

        // Bin code unique within its warehouse.
        builder.HasIndex(b => new { b.WarehouseId, b.Code }).IsUnique();
    }
}
