using ERPCore.Domain.Entities;
using ERPCore.Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class VendorConfiguration : IEntityTypeConfiguration<Vendor>
{
    public void Configure(EntityTypeBuilder<Vendor> builder)
    {
        builder.ToTable("vendors");
        builder.HasKey(v => v.VendorId);

        builder.Property(v => v.Code).IsRequired().HasMaxLength(50);
        builder.HasIndex(v => v.Code).IsUnique();

        builder.Property(v => v.Name).IsRequired().HasMaxLength(200);
        builder.Property(v => v.Terms).HasMaxLength(50);
        builder.Property(v => v.TaxReg).HasMaxLength(50);
        builder.Property(v => v.Currency).IsRequired().HasMaxLength(3).HasDefaultValue("LKR");

        builder.Property(v => v.Status)
            .HasConversion<string>().HasMaxLength(20).IsRequired()
            .HasDefaultValue(EntityStatus.Active);

        builder.Property(v => v.CreatedAt).IsRequired();

        // PostgreSQL xmin system column as the optimistic concurrency token (ETag).
        builder.Property(v => v.RowVersion).IsRowVersion();

        builder.HasIndex(v => v.Status);
    }
}
