using Microsoft.EntityFrameworkCore;

public static class EncryptPhiCommand
{
    public static async Task RunAsync(IServiceProvider services)
    {
        using var scope = services.CreateScope();
        var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
        var crypto = scope.ServiceProvider.GetRequiredService<IPhiEncryptionService>();

        var patients = await db.Patients.ToListAsync();
        foreach (var p in patients)
        {
            p.NameSearchToken = crypto.ComputeNameSearchToken(p.FirstName, p.LastName);
        }

        await db.SaveChangesAsync();
        Console.WriteLine($"Encrypted {patients.Count} patient records.");
    }
}