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

namespace Indotalent.Infrastructure.Database.MsSQL.Configuration;

public class PurchaseRequisitionItemConfiguration : IEntityTypeConfiguration<PurchaseRequisitionItem>
{
    public void Configure(EntityTypeBuilder<PurchaseRequisitionItem> builder)
    {
        builder.Property(e => e.PurchaseRequisitionId)
            .HasMaxLength(GlobalConsts.StringLengthId);

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

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

        builder.HasOne(e => e.PurchaseRequisition)
            .WithMany(e => e.PurchaseRequisitionItemList)
            .HasForeignKey(e => e.PurchaseRequisitionId)
            .OnDelete(DeleteBehavior.Cascade);

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

        builder.HasIndex(e => e.PurchaseRequisitionId);
        builder.HasIndex(e => e.ProductId);
    }
}