import numpy as np
import pandas as pd

def simulate_incorrect_ontology_attack(num_students=30):
    """
    Incorrect Ontology Attack: The prerequisite graph itself is wrong.
    """
    np.random.seed(42)
    
    results = []
    
    for student in range(num_students):
        # Simulate two scenarios
        # Scenario A: Correct ontology
        correct_progress = np.random.uniform(0.65, 0.95)
        
        # Scenario B: Wrong ontology (prerequisite missing or misplaced)
        wrong_ontology_penalty = np.random.uniform(0.15, 0.40)
        wrong_progress = correct_progress - wrong_ontology_penalty
        
        # Detection of wrong schema (simplified)
        wrong_schema_detected = wrong_ontology_penalty > 0.28
        
        results.append({
            'student': student + 1,
            'correct_ontology_progress': round(correct_progress, 3),
            'wrong_ontology_progress': round(max(0.2, wrong_progress), 3),
            'performance_degradation': round(wrong_ontology_penalty, 3),
            'wrong_schema_detected': wrong_schema_detected
        })
    
    df = pd.DataFrame(results)
    
    avg_degradation = df['performance_degradation'].mean()
    detection_rate = df['wrong_schema_detected'].mean()
    
    print("=== Incorrect Ontology Attack Simulation ===")
    print(f"Average performance degradation: {avg_degradation:.1%}")
    print(f"Wrong schema detection rate: {detection_rate:.1%}")
    
    if avg_degradation > 0.25:
        print("\n→ Significant degradation when ontology is wrong.")
    if detection_rate < 0.5:
        print("→ System often fails to detect that the prerequisite structure is incorrect.")
    
    return df

if __name__ == "__main__":
    results = simulate_incorrect_ontology_attack()
    results.to_csv('/home/workdir/artifacts/incorrect_ontology_results.csv', index=False)
    print("\nResults saved.")