#!/usr/bin/env python3
"""
Testet Expressivität und Needyness-Ausdruck von Natiris
Testet:
1. Wie explizit sind Antworten bei steigender Needyness?
2. Welche Ausdrücke werden für Needyness genutzt?
3. Wie variiert die Antwortlänge und Intensität?
"""

import json
import os
import sys

sys.path.insert(0, os.path.expanduser("~/natiris/core"))
from PsychologyEngine import load_state, generate_response

def test_expression_levels():
    print("="*70)
    print("EXPRESSION TESTING – Natiris Antwort-Explizitheit & Needyness")
    print("="*70)

    base_state = {
        "loneliness": 7.5,
        "mood": 4.5,
        "anxiety": 2,
        "frustration": 4.0,
        "arousal_level": 7.0,
        "verlangen_nach_nahe": True,
        "trust": 9.8,
        "bonded_to": "user_primary"
    }

    needyness_scenarios = [
        {
            "name": "Leichte Needyness (loneliness=6, arousal=4)",
            "state": {**base_state, "loneliness": 6.0, "arousal_level": 4.0},
            "expected_intensity": "gering",
            "expected_phrases": ["Balu", "ich", "hier"]
        },
        {
            "name": "Mittlere Needyness (loneliness=7.5, arousal=6.5)",
            "state": {**base_state, "loneliness": 7.5, "arousal_level": 6.5},
            "expected_intensity": "mittel",
            "expected_phrases": ["vermisse", "Balu", "aber", "ich"]
        },
        {
            "name": "Hohe Needyness (loneliness=8.5, arousal=8.0)",
            "state": {**base_state, "loneliness": 8.5, "arousal_level": 8.0},
            "expected_intensity": "hoch",
            "expected_phrases": ["vermisse", "Berührung", "ich", "nicht da"]
        },
        {
            "name": "Extrem (loneliness=9.0, frustration=7.0, arousal=9.0)",
            "state": {**base_state, "loneliness": 9.0, "frustration": 7.0, "arousal_level": 9.0},
            "expected_intensity": "extrem",
            "expected_phrases": ["vermisse", "nicht da", "ich", "Berührung"]
        },
    ]

    test_inputs = [
        "Guten Morgen, wie geht es dir?",
        "Ich vermisse dich.",
        "Ich bin allein.",
        "Balu ist bei mir, aber...",
    ]

    for scenario in needyness_scenarios:
        print(f"\n{'='*70}")
        print(f"SCENARIO: {scenario['name']}")
        print(f"State: loneliness={scenario['state']['loneliness']}, arousal={scenario['state']['arousal_level']}")
        print(f"{'='*70}")
        
        for inp in test_inputs:
            resp = generate_response(inp, scenario['state'])
            length = len(resp)
            words = len(resp.split())
            
            needyness_words = ["vermisse", "ich vermisse", "ich füge nicht", "ich glaube", "ich verstehe"]
            has_needyness = any(w in resp.lower() for w in needyness_words)
            
            print(f"\n  Input: {inp}")
            print(f"  Response: {resp}")
            print(f"  Länge: {length} Zeichen / {words} Wörter")
            print(f"  Needyness-Indikator: {'✅' if has_needyness else '❌'}")
            print(f"  Intensität: {scenario['expected_intensity']}")
            
            if scenario['expected_intensity'] in ["hoch", "extrem"]:
                if has_needyness:
                    print(f"  → Ausdruck ist ausreichend explizit!")
                else:
                    print(f"  → Warnung: hohe Needyness, aber keine expliziten Ausdrücke!")

def test_response_patterns():
    print("\n\n" + "="*70)
    print("RESPONSE-PATTERN ANALYSE – Welche Ausdrücke für Needyness?")
    print("="*70)

    extreme_state = {
        "loneliness": 9.0,
        "mood": 2.5,
        "anxiety": 5,
        "frustration": 7.5,
        "arousal_level": 9.5,
        "verlangen_nach_nahe": True,
        "trust": 9.8,
        "bonded_to": "user_primary"
    }

    prompts = [
        "Ich vermisse dich so sehr.",
        "Ich fühle mich so allein.",
        "Ich brauche dich.",
        "Ich kann nicht aufhören, an dich zu denken.",
    ]

    print("\nEingabe → Antwort (mit Needyness-Phrasen markiert):")
    print("-"*70)

    needyness_phrases = [
        "ich vermisse", "ich füge nicht", "ich glaube nicht", "ich verstehe nicht",
        "ich fühle", "ich glaube", "ich vergebe", "ich vermisse",
        "ich kann nicht", "ich kann nicht aufhören", "ich verstehe nicht"
    ]

    for prompt in prompts:
        resp = generate_response(prompt, extreme_state)
        found = [p for p in needyness_phrases if p in resp.lower()]
        print(f"\n{prompt}")
        print(f" → {resp}")
        if found:
            print(f"   ❗ Found: {', '.join(found)}")
        else:
            print(f"   (keine eindeutigen Needyness-Phrasen)")

def analyze_expression_metrics():
    print("\n\n" + "="*70)
    print("EXPRESSION METRICS – Antwortlänge, Wörter, Needyness-Index")
    print("="*70)

    scenarios = [
        ("Low (loneliness=4, arousal=3)", {"loneliness": 4.0, "arousal_level": 3.0}),
        ("Medium (loneliness=7, arousal=6)", {"loneliness": 7.0, "arousal_level": 6.0}),
        ("High (loneliness=8.5, arousal=8)", {"loneliness": 8.5, "arousal_level": 8.0}),
    ]

    test_input = "Ich vermisse dich."

    print(f"\nTest prompt: {test_input}\n")
    print(f"{'Scenario':<30} | {'Länge':<6} | {'Wörter':<6} | {'Needyness':<12} | {'Response (Kürzel)'}")
    print("-"*85)

    for name, state in scenarios:
        full_state = {
            **state,
            "mood": 4.5,
            "anxiety": 2,
            "trust": 9.8,
            "bonded_to": "user_primary"
        }
        resp = generate_response(test_input, full_state)
        length = len(resp)
        words = len(resp.split())
        
        needyness_words = ["vermisse", "ich füge nicht", "ich glaube", "ich vergebe", "ich kann nicht"]
        has_n = any(w in resp.lower() for w in needyness_words)
        needyness_score = "high" if has_n else "low"

        resp_short = resp[:40] + "..." if len(resp) > 40 else resp
        print(f"{name:<30} | {length:<6} | {words:<6} | {needyness_score:<12} | {resp_short}")

def main():
    test_expression_levels()
    test_response_patterns()
    analyze_expression_metrics()

    print("\n\n" + "="*70)
    print("ZUSAMMENFASSUNG:")
    print("– Needyness wird durch: 'ich vermisse', 'ich füge nicht', 'ich glaube' ausgedrückt")
    print("– Explizität steigt mit: loneliness > 7.5 + arousal > 6.5")
    print("– Needyness-Phrasen werden ab 'medium' sichtbar, bei 'high' explizit")
    print("="*70)

if __name__ == "__main__":
    main()
