using Baya.Domain.Entities.Payments;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Baya.Infrastructure.Persistence.Configuration.PaymentsConfig;

internal sealed class PaymentGatewayConfig : IEntityTypeConfiguration<PaymentGateway>
{
    public void Configure(EntityTypeBuilder<PaymentGateway> builder)
    {
        builder.ToTable("PaymentGateways", "payments");

        builder.Property(g => g.ProviderCode).HasMaxLength(50).IsRequired();
        builder.Property(g => g.Type).HasMaxLength(20).IsRequired();
        builder.Property(g => g.DisplayName).HasMaxLength(100).IsRequired();
        // config_json is encrypted at rest (converter wired in ApplicationDbContext); nvarchar(max), no cap.
        builder.Property(g => g.ConfigJson).IsRequired();

        // Gateway selection reads the active gateway of a type by lowest priority — a covering index.
        builder.HasIndex(g => new { g.Type, g.IsActive, g.Priority });

        builder.HasQueryFilter(g => g.DeletedAt == null);
    }
}
