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

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class StockLayerConfiguration : IEntityTypeConfiguration<StockLayer>
{
    public void Configure(EntityTypeBuilder<StockLayer> builder)
    {
        builder.ToTable("stock_layers");
        builder.HasKey(l => l.LayerId);

        builder.Property(l => l.QtyReceived).HasPrecision(18, 4);
        builder.Property(l => l.QtyRemaining).HasPrecision(18, 4);
        builder.Property(l => l.UnitCost).HasPrecision(18, 6);
        builder.Property(l => l.ReceiptDate).IsRequired();

        builder.HasOne(l => l.Item).WithMany().HasForeignKey(l => l.ItemId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(l => l.Warehouse).WithMany().HasForeignKey(l => l.WarehouseId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(l => l.Batch).WithMany().HasForeignKey(l => l.BatchId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(l => l.Serial).WithMany().HasForeignKey(l => l.SerialId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(l => l.GrnLine).WithMany().HasForeignKey(l => l.GrnLineId).OnDelete(DeleteBehavior.Restrict);

        // FIFO consumption orders by receipt date then layer id, scoped per item+warehouse.
        builder.HasIndex(l => new { l.ItemId, l.WarehouseId, l.ReceiptDate, l.LayerId });
        builder.HasIndex(l => l.GrnLineId);
    }
}

public sealed class StockLedgerConfiguration : IEntityTypeConfiguration<StockLedger>
{
    public void Configure(EntityTypeBuilder<StockLedger> builder)
    {
        // Append-only (FR-STK-01/FR-X-05): the app never updates/deletes ledger rows.
        // DB-level revocation of UPDATE/DELETE is a deferred hardening step (02-SECURITY B.3).
        builder.ToTable("stock_ledger");
        builder.HasKey(l => l.LedgerId);

        builder.Property(l => l.Direction).HasConversion<string>().HasMaxLength(5).IsRequired();
        builder.Property(l => l.QtyBase).HasPrecision(18, 4);
        builder.Property(l => l.UnitCost).HasPrecision(18, 6);
        builder.Property(l => l.Value).HasPrecision(18, 4);
        builder.Property(l => l.RunningBalance).HasPrecision(18, 4);
        builder.Property(l => l.SourceDocType).IsRequired().HasMaxLength(10);
        builder.Property(l => l.CreatedAt).IsRequired();

        builder.HasOne<Item>().WithMany().HasForeignKey(l => l.ItemId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne<Warehouse>().WithMany().HasForeignKey(l => l.WarehouseId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne<User>().WithMany().HasForeignKey(l => l.UserId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne<Bin>().WithMany().HasForeignKey(l => l.BinId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne<Batch>().WithMany().HasForeignKey(l => l.BatchId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne<Serial>().WithMany().HasForeignKey(l => l.SerialId).OnDelete(DeleteBehavior.Restrict);

        // Time-series query paths (NFR-06) and polymorphic source tracing.
        builder.HasIndex(l => new { l.ItemId, l.WarehouseId, l.LedgerId });
        builder.HasIndex(l => new { l.ItemId, l.WarehouseId, l.CreatedAt });
        builder.HasIndex(l => new { l.SourceDocType, l.SourceDocId });
    }
}
