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

namespace Indotalent.Infrastructure.Database.MsSQL.Configuration;

public class ProductConfiguration : IEntityTypeConfiguration<Product>
{
    public void Configure(EntityTypeBuilder<Product> builder)
    {
        builder.Property(e => e.Name)
            .HasMaxLength(GlobalConsts.StringLengthShort);

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

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

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

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

        builder.HasOne(e => e.UnitMeasure)
            .WithMany()
            .HasForeignKey(e => e.UnitMeasureId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasOne(e => e.ProductGroup)
            .WithMany()
            .HasForeignKey(e => e.ProductGroupId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasIndex(e => e.Name);
        builder.HasIndex(e => e.AutoNumber).IsUnique();
        builder.HasIndex(e => e.UnitMeasureId);
        builder.HasIndex(e => e.ProductGroupId);
    }
}