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

namespace Indotalent.Infrastructure.Database.MsSQL.Configuration;

public class PurchaseOrderConfiguration : IEntityTypeConfiguration<PurchaseOrder>
{
    public void Configure(EntityTypeBuilder<PurchaseOrder> builder)
    {
        builder.Property(e => e.AutoNumber)
            .HasMaxLength(GlobalConsts.StringLengthShort);

        builder.Property(e => e.Description)
            .HasMaxLength(GlobalConsts.StringLengthMedium);

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

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

        builder.HasOne(e => e.Vendor)
            .WithMany()
            .HasForeignKey(e => e.VendorId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasOne(e => e.Tax)
            .WithMany()
            .HasForeignKey(e => e.TaxId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasMany(e => e.PurchaseOrderItemList)
            .WithOne(e => e.PurchaseOrder)
            .HasForeignKey(e => e.PurchaseOrderId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasIndex(e => e.AutoNumber).IsUnique();
        builder.HasIndex(e => e.OrderDate);
        builder.HasIndex(e => e.VendorId);
        builder.HasIndex(e => e.TaxId);
    }
}