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

namespace Baya.Infrastructure.Persistence.Configuration.VerificationConfig;

internal sealed class VerificationStepConfig : IEntityTypeConfiguration<VerificationStep>
{
    public void Configure(EntityTypeBuilder<VerificationStep> builder)
    {
        builder.ToTable("VerificationSteps", "verif");

        builder.Property(s => s.Status)
            .HasConversion(s => s.ToCode(), s => VerificationCodes.ParseStepStatus(s))
            .HasMaxLength(20)
            .IsRequired();

        // Raw KYC-vendor response, kept for audit — no length cap (NVARCHAR(MAX)).
        builder.Property(s => s.ExternalResponseJson);
        builder.Property(s => s.FailureReason).HasMaxLength(500);
        builder.Property(s => s.IsAutomated).HasDefaultValue(false);

        // One step per (verification, step-type) — the idempotent-submit backstop.
        builder.HasIndex(s => new { s.NurseVerificationId, s.StepTypeId })
            .IsUnique()
            .HasDatabaseName("UX_VerificationSteps_Verification_StepType");

        builder.HasOne(s => s.NurseVerification)
            .WithMany(v => v.Steps)
            .HasForeignKey(s => s.NurseVerificationId)
            .IsRequired();

        builder.HasOne(s => s.StepType)
            .WithMany(t => t.Steps)
            .HasForeignKey(s => s.StepTypeId)
            .IsRequired();
    }
}
