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

namespace ERPCore.Infra.Persistence.Configurations;

/// <summary>
/// Configures the ItemType master (Color/Size/Material). Note there are deliberately no
/// relationships here — nothing references this table (docs/10 Part C.9).
/// </summary>
public sealed class ItemTypeConfiguration : IEntityTypeConfiguration<ItemType>
{
    public void Configure(EntityTypeBuilder<ItemType> builder)
    {
        builder.ToTable("item_types");
        builder.HasKey(t => t.ItemTypeId);

        builder.Property(t => t.Name).IsRequired().HasMaxLength(200);
        builder.HasIndex(t => t.Name).IsUnique();

        // false is the only safe default here: EF uses the CLR default as its "unset" sentinel,
        // so if the store default were true, inserting an explicit false would be mistaken for
        // "not set" and silently written as true. Sentinel and store default must agree.
        builder.Property(t => t.IsMeasurable).IsRequired().HasDefaultValue(false);

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

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

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

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