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

namespace Indotalent.Infrastructure.Database.MsSQL.Configuration;

public class SalesOrderItemConfiguration : IEntityTypeConfiguration<SalesOrderItem>
{
    public void Configure(EntityTypeBuilder<SalesOrderItem> builder)
    {
        builder.Property(e => e.SalesOrderId)
            .HasMaxLength(GlobalConsts.StringLengthId);

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

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

        builder.HasOne(e => e.SalesOrder)
            .WithMany(e => e.SalesOrderItemList)
            .HasForeignKey(e => e.SalesOrderId)
            .OnDelete(DeleteBehavior.Cascade);

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

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