using Microsoft.EntityFrameworkCore;

public static class DbResetHelper
{
    // Truncate all data between tests — faster than dropping and re-creating
    // the database, and preserves the schema (migrations do not re-run).
    public static async Task ResetAsync(AppDbContext db)
    {
        for (var attempt = 0; ; attempt++)
        {
            try
            {
                await db.Database.ExecuteSqlRawAsync(@"
                    DELETE FROM phi_access_logs;
                    DELETE FROM clinical_sync_conflicts;
                    DELETE FROM clinical_sync_batches;
                    DELETE FROM ward_gateways;
                    DELETE FROM clinical_sites;
                    DELETE FROM medication_administrations;
                    DELETE FROM sepsis_bundle_elements;
                    DELETE FROM sepsis_bundles;
                    DELETE FROM reconciliation_alerts;
                    DELETE FROM outbox_events;
                    DELETE FROM orders;
                    DELETE FROM alert_feedbacks;
                    DELETE FROM alert_quality_metrics;
                    DELETE FROM clinical_alerts;
                    DELETE FROM gcs_scores;
                    DELETE FROM sofa_scores;
                    DELETE FROM qsofa_evaluations;
                    DELETE FROM news2_scores;
                    DELETE FROM observations;
                    DELETE FROM external_resource_identifiers;
                    DELETE FROM encounters;
                    DELETE FROM alert_thresholds;
                    DELETE FROM clinical_audit_logs;
                    DELETE FROM clinical_users;
                    DELETE FROM simulation_runs;
                    DELETE FROM patients;
                ");
                return;
            }
            catch when (attempt < 5)
            {
                await Task.Delay(1000);
            }
        }
    }
}