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

namespace ERPCore.Infra.Persistence.Configurations;

public sealed class NumberSequenceConfiguration : IEntityTypeConfiguration<NumberSequence>
{
    public void Configure(EntityTypeBuilder<NumberSequence> builder)
    {
        builder.ToTable("number_sequences");
        builder.HasKey(s => s.SequenceId);

        builder.Property(s => s.DocType).HasColumnName("doc_type").IsRequired().HasMaxLength(10);
        builder.Property(s => s.Year).HasColumnName("year").IsRequired();
        builder.Property(s => s.LastNumber).HasColumnName("last_number").IsRequired();

        // One counter per (doc type, year); also the ON CONFLICT target for atomic issue.
        builder.HasIndex(s => new { s.DocType, s.Year }).IsUnique();
    }
}
