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 NurseCredentialConfig : IEntityTypeConfiguration<NurseCredential>
{
    public void Configure(EntityTypeBuilder<NurseCredential> builder)
    {
        builder.ToTable("NurseCredentials", "verif");

        builder.Property(c => c.CredentialType).HasMaxLength(50).IsRequired();
        // credential_number is encrypted at rest (converter wired in ApplicationDbContext) — never serialized.
        builder.Property(c => c.CredentialNumber).HasMaxLength(256).IsRequired();
        builder.Property(c => c.HolderNameSnapshot).HasMaxLength(200).IsRequired();
        builder.Property(c => c.IssuingAuthority).HasMaxLength(200).IsRequired();
        builder.Property(c => c.VerificationSource).HasMaxLength(300);
        builder.Property(c => c.VerificationMethod).HasMaxLength(20).IsRequired();

        // Badge + renewal reads: the nurse's non-expired credentials by type.
        builder.HasIndex(c => new { c.NurseId, c.CredentialType });

        builder.HasOne<NurseProfile>()
            .WithMany()
            .HasForeignKey(c => c.NurseId)
            .IsRequired();

        builder.HasOne<User>()
            .WithMany()
            .HasForeignKey(c => c.VerifiedByAdminId)
            .IsRequired(false);

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