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

namespace Baya.Infrastructure.Persistence.Configuration.VerificationConfig;

internal sealed class NurseVerificationConfig : IEntityTypeConfiguration<NurseVerification>
{
    public void Configure(EntityTypeBuilder<NurseVerification> builder)
    {
        builder.ToTable("NurseVerifications", "verif");

        // Status persists as its stable snake_case code — readable and queryable (WHERE status = 'approved').
        builder.Property(v => v.Status)
            .HasConversion(s => s.ToCode(), s => VerificationCodes.ParseStatus(s))
            .HasMaxLength(20)
            .IsRequired();

        builder.Property(v => v.RejectionReason).HasMaxLength(1000);
        builder.Property(v => v.InternalNotes).HasMaxLength(2000);

        // 1:1 with the nurse profile — the sole verification header per nurse.
        builder.HasIndex(v => v.NurseId).IsUnique();
        builder.HasOne(v => v.Nurse)
            .WithMany()
            .HasForeignKey(v => v.NurseId)
            .IsRequired();

        builder.HasOne<User>()
            .WithMany()
            .HasForeignKey(v => v.ReviewedByAdminId)
            .IsRequired(false);

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