using Apollinare.Domain.Entities;
using Apollinare.Domain.Entities.Warehouse;
using Microsoft.EntityFrameworkCore;

namespace Apollinare.Infrastructure.Data;

public class AppollinareDbContext : DbContext
{
    public AppollinareDbContext(DbContextOptions<AppollinareDbContext> options) : base(options)
    {
    }

    public DbSet<Cliente> Clienti => Set<Cliente>();
    public DbSet<Location> Location => Set<Location>();
    public DbSet<Evento> Eventi => Set<Evento>();
    public DbSet<TipoEvento> TipiEvento => Set<TipoEvento>();
    public DbSet<TipoPasto> TipiPasto => Set<TipoPasto>();
    public DbSet<TipoOspite> TipiOspite => Set<TipoOspite>();
    public DbSet<TipoRisorsa> TipiRisorsa => Set<TipoRisorsa>();
    public DbSet<TipoMateriale> TipiMateriale => Set<TipoMateriale>();
    public DbSet<CodiceCategoria> CodiciCategoria => Set<CodiceCategoria>();
    public DbSet<Risorsa> Risorse => Set<Risorsa>();
    public DbSet<Articolo> Articoli => Set<Articolo>();
    public DbSet<EventoDettaglioOspiti> EventiDettaglioOspiti => Set<EventoDettaglioOspiti>();
    public DbSet<EventoDettaglioPrelievo> EventiDettaglioPrelievo => Set<EventoDettaglioPrelievo>();
    public DbSet<EventoDettaglioRisorsa> EventiDettaglioRisorse => Set<EventoDettaglioRisorsa>();
    public DbSet<EventoAcconto> EventiAcconti => Set<EventoAcconto>();
    public DbSet<EventoAltroCosto> EventiAltriCosti => Set<EventoAltroCosto>();
    public DbSet<EventoAllegato> EventiAllegati => Set<EventoAllegato>();
    public DbSet<EventoDegustazione> EventiDegustazioni => Set<EventoDegustazione>();
    public DbSet<Configurazione> Configurazioni => Set<Configurazione>();
    public DbSet<Utente> Utenti => Set<Utente>();

    // Report entities
    public DbSet<ReportTemplate> ReportTemplates => Set<ReportTemplate>();
    public DbSet<ReportFont> ReportFonts => Set<ReportFont>();
    public DbSet<ReportImage> ReportImages => Set<ReportImage>();
    public DbSet<VirtualDataset> VirtualDatasets => Set<VirtualDataset>();

    // Module system entities
    public DbSet<AppModule> AppModules => Set<AppModule>();
    public DbSet<ModuleSubscription> ModuleSubscriptions => Set<ModuleSubscription>();

    // Auto Code system
    public DbSet<AutoCode> AutoCodes => Set<AutoCode>();

    // Custom Fields system
    public DbSet<CustomFieldDefinition> CustomFieldDefinitions => Set<CustomFieldDefinition>();

    // Warehouse module entities
    public DbSet<WarehouseLocation> WarehouseLocations => Set<WarehouseLocation>();
    public DbSet<WarehouseArticle> WarehouseArticles => Set<WarehouseArticle>();
    public DbSet<WarehouseArticleCategory> WarehouseArticleCategories => Set<WarehouseArticleCategory>();
    public DbSet<ArticleBatch> ArticleBatches => Set<ArticleBatch>();
    public DbSet<ArticleSerial> ArticleSerials => Set<ArticleSerial>();
    public DbSet<ArticleBarcode> ArticleBarcodes => Set<ArticleBarcode>();
    public DbSet<StockLevel> StockLevels => Set<StockLevel>();
    public DbSet<StockMovement> StockMovements => Set<StockMovement>();
    public DbSet<StockMovementLine> StockMovementLines => Set<StockMovementLine>();
    public DbSet<MovementReason> MovementReasons => Set<MovementReason>();
    public DbSet<StockValuation> StockValuations => Set<StockValuation>();
    public DbSet<StockValuationLayer> StockValuationLayers => Set<StockValuationLayer>();
    public DbSet<InventoryCount> InventoryCounts => Set<InventoryCount>();
    public DbSet<InventoryCountLine> InventoryCountLines => Set<InventoryCountLine>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Cliente
        modelBuilder.Entity<Cliente>(entity =>
        {
            entity.HasIndex(e => e.RagioneSociale);
            entity.HasIndex(e => e.PartitaIva);
        });

        // Location
        modelBuilder.Entity<Location>(entity =>
        {
            entity.HasIndex(e => e.Nome);
        });

        // Evento
        modelBuilder.Entity<Evento>(entity =>
        {
            entity.HasIndex(e => e.DataEvento);
            entity.HasIndex(e => e.Stato);
            entity.HasIndex(e => e.Codice);

            entity.HasOne(e => e.Cliente)
                .WithMany(c => c.Eventi)
                .HasForeignKey(e => e.ClienteId)
                .OnDelete(DeleteBehavior.SetNull);

            entity.HasOne(e => e.Location)
                .WithMany(l => l.Eventi)
                .HasForeignKey(e => e.LocationId)
                .OnDelete(DeleteBehavior.SetNull);

            entity.HasOne(e => e.TipoEvento)
                .WithMany(t => t.Eventi)
                .HasForeignKey(e => e.TipoEventoId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // TipoEvento
        modelBuilder.Entity<TipoEvento>(entity =>
        {
            entity.HasOne(e => e.TipoPasto)
                .WithMany(t => t.TipiEvento)
                .HasForeignKey(e => e.TipoPastoId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // Articolo
        modelBuilder.Entity<Articolo>(entity =>
        {
            entity.HasIndex(e => e.Codice).IsUnique();

            entity.HasOne(e => e.TipoMateriale)
                .WithMany(t => t.Articoli)
                .HasForeignKey(e => e.TipoMaterialeId)
                .OnDelete(DeleteBehavior.SetNull);

            entity.HasOne(e => e.Categoria)
                .WithMany(c => c.Articoli)
                .HasForeignKey(e => e.CategoriaId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // Risorsa
        modelBuilder.Entity<Risorsa>(entity =>
        {
            entity.HasOne(e => e.TipoRisorsa)
                .WithMany(t => t.Risorse)
                .HasForeignKey(e => e.TipoRisorsaId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // EventoDettaglioOspiti
        modelBuilder.Entity<EventoDettaglioOspiti>(entity =>
        {
            entity.HasOne(e => e.Evento)
                .WithMany(ev => ev.DettagliOspiti)
                .HasForeignKey(e => e.EventoId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(e => e.TipoOspite)
                .WithMany(t => t.DettagliOspiti)
                .HasForeignKey(e => e.TipoOspiteId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        // EventoDettaglioPrelievo
        modelBuilder.Entity<EventoDettaglioPrelievo>(entity =>
        {
            entity.HasOne(e => e.Evento)
                .WithMany(ev => ev.DettagliPrelievo)
                .HasForeignKey(e => e.EventoId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(e => e.Articolo)
                .WithMany(a => a.DettagliPrelievo)
                .HasForeignKey(e => e.ArticoloId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        // EventoDettaglioRisorsa
        modelBuilder.Entity<EventoDettaglioRisorsa>(entity =>
        {
            entity.HasOne(e => e.Evento)
                .WithMany(ev => ev.DettagliRisorse)
                .HasForeignKey(e => e.EventoId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(e => e.Risorsa)
                .WithMany(r => r.DettagliRisorse)
                .HasForeignKey(e => e.RisorsaId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        // EventoAcconto
        modelBuilder.Entity<EventoAcconto>(entity =>
        {
            entity.HasOne(e => e.Evento)
                .WithMany(ev => ev.Acconti)
                .HasForeignKey(e => e.EventoId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        // EventoAltroCosto
        modelBuilder.Entity<EventoAltroCosto>(entity =>
        {
            entity.HasOne(e => e.Evento)
                .WithMany(ev => ev.AltriCosti)
                .HasForeignKey(e => e.EventoId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        // EventoAllegato
        modelBuilder.Entity<EventoAllegato>(entity =>
        {
            entity.HasOne(e => e.Evento)
                .WithMany(ev => ev.Allegati)
                .HasForeignKey(e => e.EventoId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        // EventoDegustazione
        modelBuilder.Entity<EventoDegustazione>(entity =>
        {
            entity.HasOne(e => e.Evento)
                .WithMany(ev => ev.Degustazioni)
                .HasForeignKey(e => e.EventoId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        // Configurazione
        modelBuilder.Entity<Configurazione>(entity =>
        {
            entity.HasIndex(e => e.Chiave).IsUnique();
        });

        // Utente
        modelBuilder.Entity<Utente>(entity =>
        {
            entity.HasIndex(e => e.Username).IsUnique();
        });

        // ReportTemplate
        modelBuilder.Entity<ReportTemplate>(entity =>
        {
            entity.HasIndex(e => e.Nome);
            entity.HasIndex(e => e.Categoria);
        });

        // ReportFont
        modelBuilder.Entity<ReportFont>(entity =>
        {
            entity.HasIndex(e => e.FontFamily);
        });

        // ReportImage
        modelBuilder.Entity<ReportImage>(entity =>
        {
            entity.HasIndex(e => e.Categoria);
        });

        // VirtualDataset
        modelBuilder.Entity<VirtualDataset>(entity =>
        {
            entity.HasIndex(e => e.Nome).IsUnique();
            entity.HasIndex(e => e.Categoria);
        });

        // AppModule
        modelBuilder.Entity<AppModule>(entity =>
        {
            entity.HasIndex(e => e.Code).IsUnique();
            entity.HasIndex(e => e.SortOrder);

            entity.Property(e => e.BasePrice)
                .HasPrecision(18, 2);

            entity.Property(e => e.MonthlyMultiplier)
                .HasPrecision(5, 2);
        });

        // ModuleSubscription
        modelBuilder.Entity<ModuleSubscription>(entity =>
        {
            entity.HasIndex(e => e.ModuleId).IsUnique();

            entity.Property(e => e.PaidPrice)
                .HasPrecision(18, 2);

            entity.HasOne(e => e.Module)
                .WithOne(m => m.Subscription)
                .HasForeignKey<ModuleSubscription>(e => e.ModuleId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        // AutoCode
        modelBuilder.Entity<AutoCode>(entity =>
        {
            entity.HasIndex(e => e.EntityCode).IsUnique();
            entity.HasIndex(e => e.ModuleCode);
        });

        // CustomFieldDefinition
        modelBuilder.Entity<CustomFieldDefinition>(entity =>
        {
            entity.HasIndex(e => new { e.EntityName, e.FieldName }).IsUnique();
            entity.HasIndex(e => e.EntityName);
        });

        // ===============================================
        // WAREHOUSE MODULE ENTITIES
        // ===============================================

        // WarehouseLocation
        modelBuilder.Entity<WarehouseLocation>(entity =>
        {
            entity.ToTable("WarehouseLocations");
            entity.HasIndex(e => e.Code).IsUnique();
            entity.HasIndex(e => e.IsDefault);
            entity.HasIndex(e => e.IsActive);
        });

        // WarehouseArticleCategory
        modelBuilder.Entity<WarehouseArticleCategory>(entity =>
        {
            entity.ToTable("WarehouseArticleCategories");
            entity.HasIndex(e => e.Code).IsUnique();
            entity.HasIndex(e => e.ParentCategoryId);
            entity.HasIndex(e => e.FullPath);

            entity.HasOne(e => e.ParentCategory)
                .WithMany(c => c.ChildCategories)
                .HasForeignKey(e => e.ParentCategoryId)
                .OnDelete(DeleteBehavior.Restrict);
        });

        // WarehouseArticle
        modelBuilder.Entity<WarehouseArticle>(entity =>
        {
            entity.ToTable("WarehouseArticles");
            entity.HasIndex(e => e.Code).IsUnique();
            entity.HasIndex(e => e.Barcode);
            entity.HasIndex(e => e.CategoryId);
            entity.HasIndex(e => e.IsActive);

            entity.Property(e => e.StandardCost).HasPrecision(18, 4);
            entity.Property(e => e.LastPurchaseCost).HasPrecision(18, 4);
            entity.Property(e => e.WeightedAverageCost).HasPrecision(18, 4);
            entity.Property(e => e.BaseSellingPrice).HasPrecision(18, 4);
            entity.Property(e => e.MinimumStock).HasPrecision(18, 4);
            entity.Property(e => e.MaximumStock).HasPrecision(18, 4);
            entity.Property(e => e.ReorderPoint).HasPrecision(18, 4);
            entity.Property(e => e.ReorderQuantity).HasPrecision(18, 4);
            entity.Property(e => e.UnitConversionFactor).HasPrecision(18, 6);
            entity.Property(e => e.Weight).HasPrecision(18, 4);
            entity.Property(e => e.Volume).HasPrecision(18, 6);

            entity.HasOne(e => e.Category)
                .WithMany(c => c.Articles)
                .HasForeignKey(e => e.CategoryId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // ArticleBatch
        modelBuilder.Entity<ArticleBatch>(entity =>
        {
            entity.ToTable("ArticleBatches");
            entity.HasIndex(e => new { e.ArticleId, e.BatchNumber }).IsUnique();
            entity.HasIndex(e => e.ExpiryDate);
            entity.HasIndex(e => e.Status);

            entity.Property(e => e.UnitCost).HasPrecision(18, 4);
            entity.Property(e => e.InitialQuantity).HasPrecision(18, 4);
            entity.Property(e => e.CurrentQuantity).HasPrecision(18, 4);
            entity.Property(e => e.ReservedQuantity).HasPrecision(18, 4);

            entity.HasOne(e => e.Article)
                .WithMany(a => a.Batches)
                .HasForeignKey(e => e.ArticleId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        // ArticleSerial
        modelBuilder.Entity<ArticleSerial>(entity =>
        {
            entity.ToTable("ArticleSerials");
            entity.HasIndex(e => new { e.ArticleId, e.SerialNumber }).IsUnique();
            entity.HasIndex(e => e.Status);
            entity.HasIndex(e => e.CurrentWarehouseId);

            entity.Property(e => e.UnitCost).HasPrecision(18, 4);

            entity.HasOne(e => e.Article)
                .WithMany(a => a.Serials)
                .HasForeignKey(e => e.ArticleId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(e => e.Batch)
                .WithMany(b => b.Serials)
                .HasForeignKey(e => e.BatchId)
                .OnDelete(DeleteBehavior.SetNull);

            entity.HasOne(e => e.CurrentWarehouse)
                .WithMany()
                .HasForeignKey(e => e.CurrentWarehouseId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // ArticleBarcode
        modelBuilder.Entity<ArticleBarcode>(entity =>
        {
            entity.ToTable("ArticleBarcodes");
            entity.HasIndex(e => e.Barcode).IsUnique();
            entity.HasIndex(e => e.ArticleId);

            entity.Property(e => e.Quantity).HasPrecision(18, 4);

            entity.HasOne(e => e.Article)
                .WithMany(a => a.Barcodes)
                .HasForeignKey(e => e.ArticleId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        // StockLevel
        modelBuilder.Entity<StockLevel>(entity =>
        {
            entity.ToTable("StockLevels");
            entity.HasIndex(e => new { e.ArticleId, e.WarehouseId, e.BatchId }).IsUnique();
            entity.HasIndex(e => e.WarehouseId);
            entity.HasIndex(e => e.LocationCode);

            entity.Property(e => e.Quantity).HasPrecision(18, 4);
            entity.Property(e => e.ReservedQuantity).HasPrecision(18, 4);
            entity.Property(e => e.OnOrderQuantity).HasPrecision(18, 4);
            entity.Property(e => e.StockValue).HasPrecision(18, 4);
            entity.Property(e => e.UnitCost).HasPrecision(18, 4);

            entity.HasOne(e => e.Article)
                .WithMany(a => a.StockLevels)
                .HasForeignKey(e => e.ArticleId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(e => e.Warehouse)
                .WithMany(w => w.StockLevels)
                .HasForeignKey(e => e.WarehouseId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(e => e.Batch)
                .WithMany(b => b.StockLevels)
                .HasForeignKey(e => e.BatchId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // MovementReason
        modelBuilder.Entity<MovementReason>(entity =>
        {
            entity.ToTable("MovementReasons");
            entity.HasIndex(e => e.Code).IsUnique();
            entity.HasIndex(e => e.MovementType);
            entity.HasIndex(e => e.IsActive);
        });

        // StockMovement
        modelBuilder.Entity<StockMovement>(entity =>
        {
            entity.ToTable("StockMovements");
            entity.HasIndex(e => e.DocumentNumber).IsUnique();
            entity.HasIndex(e => e.MovementDate);
            entity.HasIndex(e => e.Type);
            entity.HasIndex(e => e.Status);
            entity.HasIndex(e => e.ExternalReference);

            entity.Property(e => e.TotalValue).HasPrecision(18, 4);

            entity.HasOne(e => e.SourceWarehouse)
                .WithMany(w => w.SourceMovements)
                .HasForeignKey(e => e.SourceWarehouseId)
                .OnDelete(DeleteBehavior.Restrict);

            entity.HasOne(e => e.DestinationWarehouse)
                .WithMany(w => w.DestinationMovements)
                .HasForeignKey(e => e.DestinationWarehouseId)
                .OnDelete(DeleteBehavior.Restrict);

            entity.HasOne(e => e.Reason)
                .WithMany(r => r.Movements)
                .HasForeignKey(e => e.ReasonId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // StockMovementLine
        modelBuilder.Entity<StockMovementLine>(entity =>
        {
            entity.ToTable("StockMovementLines");
            entity.HasIndex(e => new { e.MovementId, e.LineNumber }).IsUnique();
            entity.HasIndex(e => e.ArticleId);
            entity.HasIndex(e => e.BatchId);
            entity.HasIndex(e => e.SerialId);

            entity.Property(e => e.Quantity).HasPrecision(18, 4);
            entity.Property(e => e.UnitCost).HasPrecision(18, 4);
            entity.Property(e => e.LineValue).HasPrecision(18, 4);

            entity.HasOne(e => e.Movement)
                .WithMany(m => m.Lines)
                .HasForeignKey(e => e.MovementId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(e => e.Article)
                .WithMany(a => a.MovementLines)
                .HasForeignKey(e => e.ArticleId)
                .OnDelete(DeleteBehavior.Restrict);

            entity.HasOne(e => e.Batch)
                .WithMany(b => b.MovementLines)
                .HasForeignKey(e => e.BatchId)
                .OnDelete(DeleteBehavior.SetNull);

            entity.HasOne(e => e.Serial)
                .WithMany(s => s.MovementLines)
                .HasForeignKey(e => e.SerialId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // StockValuation
        modelBuilder.Entity<StockValuation>(entity =>
        {
            entity.ToTable("StockValuations");
            entity.HasIndex(e => new { e.Period, e.ArticleId, e.WarehouseId }).IsUnique();
            entity.HasIndex(e => e.ValuationDate);
            entity.HasIndex(e => e.IsClosed);

            entity.Property(e => e.Quantity).HasPrecision(18, 4);
            entity.Property(e => e.UnitCost).HasPrecision(18, 4);
            entity.Property(e => e.TotalValue).HasPrecision(18, 4);
            entity.Property(e => e.InboundQuantity).HasPrecision(18, 4);
            entity.Property(e => e.InboundValue).HasPrecision(18, 4);
            entity.Property(e => e.OutboundQuantity).HasPrecision(18, 4);
            entity.Property(e => e.OutboundValue).HasPrecision(18, 4);

            entity.HasOne(e => e.Article)
                .WithMany()
                .HasForeignKey(e => e.ArticleId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(e => e.Warehouse)
                .WithMany()
                .HasForeignKey(e => e.WarehouseId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // StockValuationLayer
        modelBuilder.Entity<StockValuationLayer>(entity =>
        {
            entity.ToTable("StockValuationLayers");
            entity.HasIndex(e => new { e.ArticleId, e.WarehouseId, e.LayerDate });
            entity.HasIndex(e => e.IsExhausted);

            entity.Property(e => e.OriginalQuantity).HasPrecision(18, 4);
            entity.Property(e => e.RemainingQuantity).HasPrecision(18, 4);
            entity.Property(e => e.UnitCost).HasPrecision(18, 4);

            entity.HasOne(e => e.Article)
                .WithMany()
                .HasForeignKey(e => e.ArticleId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(e => e.Warehouse)
                .WithMany()
                .HasForeignKey(e => e.WarehouseId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(e => e.Batch)
                .WithMany()
                .HasForeignKey(e => e.BatchId)
                .OnDelete(DeleteBehavior.SetNull);

            entity.HasOne(e => e.SourceMovement)
                .WithMany()
                .HasForeignKey(e => e.SourceMovementId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // InventoryCount
        modelBuilder.Entity<InventoryCount>(entity =>
        {
            entity.ToTable("InventoryCounts");
            entity.HasIndex(e => e.Code).IsUnique();
            entity.HasIndex(e => e.InventoryDate);
            entity.HasIndex(e => e.Status);

            entity.Property(e => e.PositiveDifferenceValue).HasPrecision(18, 4);
            entity.Property(e => e.NegativeDifferenceValue).HasPrecision(18, 4);

            entity.HasOne(e => e.Warehouse)
                .WithMany()
                .HasForeignKey(e => e.WarehouseId)
                .OnDelete(DeleteBehavior.SetNull);

            entity.HasOne(e => e.Category)
                .WithMany()
                .HasForeignKey(e => e.CategoryId)
                .OnDelete(DeleteBehavior.SetNull);

            entity.HasOne(e => e.AdjustmentMovement)
                .WithMany()
                .HasForeignKey(e => e.AdjustmentMovementId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        // InventoryCountLine
        modelBuilder.Entity<InventoryCountLine>(entity =>
        {
            entity.ToTable("InventoryCountLines");
            entity.HasIndex(e => new { e.InventoryCountId, e.ArticleId, e.WarehouseId, e.BatchId }).IsUnique();
            entity.HasIndex(e => e.ArticleId);

            entity.Property(e => e.TheoreticalQuantity).HasPrecision(18, 4);
            entity.Property(e => e.CountedQuantity).HasPrecision(18, 4);
            entity.Property(e => e.SecondCountQuantity).HasPrecision(18, 4);
            entity.Property(e => e.UnitCost).HasPrecision(18, 4);

            entity.HasOne(e => e.InventoryCount)
                .WithMany(i => i.Lines)
                .HasForeignKey(e => e.InventoryCountId)
                .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(e => e.Article)
                .WithMany()
                .HasForeignKey(e => e.ArticleId)
                .OnDelete(DeleteBehavior.Restrict);

            entity.HasOne(e => e.Warehouse)
                .WithMany()
                .HasForeignKey(e => e.WarehouseId)
                .OnDelete(DeleteBehavior.Restrict);

            entity.HasOne(e => e.Batch)
                .WithMany()
                .HasForeignKey(e => e.BatchId)
                .OnDelete(DeleteBehavior.SetNull);
        });
    }
}
