using Microsoft.EntityFrameworkCore;

public static class PatientPhiConverterConfigurator
{
    public static void ConfigurePhiConverters(
        this ModelBuilder modelBuilder,
        IPhiEncryptionService crypto)
    {
        var entity = modelBuilder.Entity<Patient>();

        entity.Property(p => p.FirstName)
            .HasColumnType("text")
            .HasConversion(
                v => crypto.Encrypt(v),
                v => crypto.Decrypt(v));

        entity.Property(p => p.LastName)
            .HasColumnType("text")
            .HasConversion(
                v => crypto.Encrypt(v),
                v => crypto.Decrypt(v));

        entity.Property(p => p.Allergies)
            .HasConversion(
                v => v == null ? null! : crypto.Encrypt(v),
                v => v == null ? null : crypto.Decrypt(v));

        entity.Property(p => p.EmergencyContactName)
            .HasColumnType("text")
            .HasConversion(
                v => v == null ? null! : crypto.Encrypt(v),
                v => v == null ? null : crypto.Decrypt(v));

        entity.Property(p => p.EmergencyContactPhone)
            .HasColumnType("text")
            .HasConversion(
                v => v == null ? null! : crypto.Encrypt(v),
                v => v == null ? null : crypto.Decrypt(v));

        // DateOfBirth stored as encrypted ISO string
        entity.Property(p => p.DateOfBirth)
            .HasConversion(
                v => crypto.Encrypt(v.ToString("yyyy-MM-dd")),
                v => DateOnly.Parse(crypto.Decrypt(v)));
    }
}