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

namespace Baya.Infrastructure.Persistence.Configuration.PaymentsConfig;

internal sealed class PaymentWebhookEventConfig : IEntityTypeConfiguration<PaymentWebhookEvent>
{
    public void Configure(EntityTypeBuilder<PaymentWebhookEvent> builder)
    {
        builder.ToTable("PaymentWebhookEvents", "payments");

        builder.Property(e => e.ProviderCode).HasMaxLength(50).IsRequired();
        builder.Property(e => e.ExternalEventId).HasMaxLength(200).IsRequired();
        builder.Property(e => e.EventType).HasMaxLength(80).IsRequired();
        builder.Property(e => e.ProcessingStatus).HasMaxLength(20).IsRequired();
        // payload_json is nvarchar(max) (raw callback); no length cap.
        builder.Property(e => e.PayloadJson).IsRequired();

        // THE idempotency key — a duplicate provider event can never insert twice, so a replay can never
        // double-confirm/double-count. No soft-delete: this store is append/upsert-only.
        builder.HasIndex(e => new { e.ProviderCode, e.ExternalEventId }).IsUnique();
    }
}
