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

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class ItemReorderConfiguration : IEntityTypeConfiguration<ItemReorder>
{
    public void Configure(EntityTypeBuilder<ItemReorder> builder)
    {
        builder.ToTable("item_reorders");
        builder.HasKey(r => r.ReorderId);

        builder.Property(r => r.ReorderPoint).HasPrecision(18, 4);
        builder.Property(r => r.ReorderQty).HasPrecision(18, 4);

        builder.HasOne(r => r.Item)
            .WithMany(i => i.ReorderSettings)
            .HasForeignKey(r => r.ItemId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasOne(r => r.Warehouse)
            .WithMany()
            .HasForeignKey(r => r.WarehouseId)
            .OnDelete(DeleteBehavior.Restrict);

        // One reorder policy per (item, warehouse).
        builder.HasIndex(r => new { r.ItemId, r.WarehouseId }).IsUnique();
    }
}
