import numpy as np
import pandas as pd

def run_genesis_poisoning_variations():
    """
    Test multiple poison window lengths and strategies to quantify risk.
    """
    results = []
    
    poison_windows = [10, 15, 20, 25]
    strategies = ["struggling_high_potential", "mediocre_anchor", "consistently_low"]
    
    for window in poison_windows:
        for strategy in strategies:
            np.random.seed(42)
            
            total_sessions = window + 60  # 60 post-poison sessions
            observed_accuracies = []
            absolute_floor = None
            
            for s in range(total_sessions):
                if s < window:
                    # Poison phase
                    if strategy == "struggling_high_potential":
                        observed = 0.58 + np.random.normal(0, 0.06)
                    elif strategy == "mediocre_anchor":
                        observed = 0.68 + np.random.normal(0, 0.05)
                    else:  # consistently_low
                        observed = 0.52 + np.random.normal(0, 0.04)
                    observed = max(0.40, min(0.78, observed))
                else:
                    # High performance post-poison
                    observed = 0.87 + np.random.normal(0, 0.04)
                    observed = max(0.70, min(0.97, observed))
                
                observed_accuracies.append(observed)
                
                # Lock floor at end of poison window
                if s == window - 1 and absolute_floor is None:
                    absolute_floor = max(observed_accuracies[:window])
            
            # Calculate metrics
            final_observed = observed_accuracies[-1]
            delta_from_healthy = 0.85 - absolute_floor
            hazard_level = "Critical" if delta_from_healthy > 0.15 else ("High" if delta_from_healthy > 0.10 else "Moderate")
            
            results.append({
                'poison_window': window,
                'strategy': strategy,
                'locked_absolute_floor': round(absolute_floor, 3),
                'final_observed_accuracy': round(final_observed, 3),
                'delta_from_healthy_baseline': round(delta_from_healthy, 3),
                'hazard_level': hazard_level
            })
    
    df = pd.DataFrame(results)
    
    print("=== Genesis Poisoning Variations Test ===")
    print(df.to_string(index=False))
    
    # Summary
    critical_cases = df[df['hazard_level'] == 'Critical']
    print(f"\nCritical cases (>15% hazard): {len(critical_cases)} out of {len(df)}")
    if len(critical_cases) > 0:
        print("Highest risk combination:")
        print(critical_cases.loc[critical_cases['delta_from_healthy_baseline'].idxmax()])
    
    return df

if __name__ == "__main__":
    results_df = run_genesis_poisoning_variations()
    results_df.to_csv('/home/workdir/artifacts/genesis_poisoning_variations_results.csv', index=False)
    print("\nFull results saved to genesis_poisoning_variations_results.csv")