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

namespace Indotalent.Infrastructure.Database.MsSQL.Configuration;

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

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

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

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

        builder.HasOne(e => e.Customer)
            .WithMany()
            .HasForeignKey(e => e.CustomerId)
            .OnDelete(DeleteBehavior.NoAction);

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

        builder.HasMany(e => e.SalesQuotationItemList)
            .WithOne(e => e.SalesQuotation)
            .HasForeignKey(e => e.SalesQuotationId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasIndex(e => e.AutoNumber).IsUnique();
        builder.HasIndex(e => e.QuotationDate);
        builder.HasIndex(e => e.CustomerId);
        builder.HasIndex(e => e.TaxId);
    }
}