using Indotalent.Data.Entities;
using Indotalent.Shared.Consts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Indotalent.Infrastructure.Database.MsSQL.Configuration;

public class InventoryTransactionConfiguration : IEntityTypeConfiguration<InventoryTransaction>
{
    public void Configure(EntityTypeBuilder<InventoryTransaction> builder)
    {
        builder.Property(e => e.ModuleId)
            .HasMaxLength(GlobalConsts.StringLengthId);

        builder.Property(e => e.ModuleName)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.ModuleCode)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.ModuleNumber)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.AutoNumber)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.WarehouseId)
            .HasMaxLength(GlobalConsts.StringLengthId);

        builder.Property(e => e.ProductId)
            .HasMaxLength(GlobalConsts.StringLengthId);

        builder.Property(e => e.WarehouseFromId)
            .HasMaxLength(GlobalConsts.StringLengthId);

        builder.Property(e => e.WarehouseToId)
            .HasMaxLength(GlobalConsts.StringLengthId);

        builder.HasOne(e => e.Warehouse)
            .WithMany()
            .HasForeignKey(e => e.WarehouseId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasOne(e => e.Product)
            .WithMany()
            .HasForeignKey(e => e.ProductId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasOne(e => e.WarehouseFrom)
            .WithMany()
            .HasForeignKey(e => e.WarehouseFromId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasOne(e => e.WarehouseTo)
            .WithMany()
            .HasForeignKey(e => e.WarehouseToId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasIndex(e => new { e.TenantId, e.AutoNumber }).IsUnique();
        builder.HasIndex(e => e.MovementDate);
        builder.HasIndex(e => e.WarehouseId);
        builder.HasIndex(e => e.ProductId);
    }
}