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

namespace Indotalent.Infrastructure.Database.MsSQL.Configuration;

public class PurchaseOrderItemConfiguration : IEntityTypeConfiguration<PurchaseOrderItem>
{
    public void Configure(EntityTypeBuilder<PurchaseOrderItem> builder)
    {
        builder.Property(e => e.PurchaseOrderId)
            .HasMaxLength(GlobalConsts.StringLengthId);

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

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

        builder.HasOne(e => e.PurchaseOrder)
            .WithMany(e => e.PurchaseOrderItemList)
            .HasForeignKey(e => e.PurchaseOrderId)
            .OnDelete(DeleteBehavior.Cascade);

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

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