#!/usr/bin/env python3
"""
Natiris Update Script – Aktiviert NaturalLanguageEngine
"""

import os
import sys
import shutil
import json

CORE_DIR = os.path.expanduser("~/natiris/core")

def backup_old():
    """Sichere alte Versionen"""
    files_to_backup = [
        "PsychologyEngine.py",
        "ExpressionEngine.py",
        "webui_chat.py"
    ]
    
    for file in files_to_backup:
        src = os.path.join(CORE_DIR, file)
        dst = os.path.join(CORE_DIR, f"{file}.backup_v1")
        if os.path.exists(src) and not os.path.exists(dst):
            shutil.copy2(src, dst)
            print(f"✓ Backup erstellt: {file}.backup_v1")

def activate_v2():
    """Aktiviert v2 Module"""
    
    # PsychologyEngine v2 aktivieren
    v2_path = os.path.join(CORE_DIR, "PsychologyEngine_v2.py")
    original_path = os.path.join(CORE_DIR, "PsychologyEngine.py")
    
    if os.path.exists(v2_path):
        # Tausche Dateien
        if os.path.exists(original_path):
            os.rename(original_path, os.path.join(CORE_DIR, "PsychologyEngine_old.py"))
        os.rename(v2_path, original_path)
        print("✓ PsychologyEngine v2 aktiviert")
    
    # ExpressionEngine v2 aktivieren
    expr_v2 = os.path.join(CORE_DIR, "ExpressionEngine_v2.py")
    expr_orig = os.path.join(CORE_DIR, "ExpressionEngine.py")
    
    if os.path.exists(expr_v2):
        if os.path.exists(expr_orig):
            os.rename(expr_orig, os.path.join(CORE_DIR, "ExpressionEngine_old.py"))
        os.rename(expr_v2, expr_orig)
        print("✓ ExpressionEngine v2 aktiviert")
    
    # WebUI v2 aktivieren (optional, parallel verfügbar)
    webui_v2 = os.path.join(CORE_DIR, "webui_chat_v2.py")
    if os.path.exists(webui_v2):
        # Kopiere als zusätzliche Option
        target = os.path.join(CORE_DIR, "webui_natural.py")
        shutil.copy2(webui_v2, target)
        print("✓ WebUI Natural verfügbar als webui_natural.py")

def init_conversation_memory():
    """Initialisiert das Conversation Memory"""
    mem_path = os.path.expanduser("~/natiris/memory/conversation_thread.json")
    if not os.path.exists(mem_path):
        with open(mem_path, "w") as f:
            json.dump([], f)
        print("✓ Conversation Memory initialisiert")

def update_status():
    """Aktualisiert Projektstatus"""
    status_path = os.path.expanduser("~/natiris/core/STATUS_CURRENT.json")
    try:
        with open(status_path) as f:
            status = json.load(f)
        
        status["natural_language_engine"] = "v2.0-aktiviert"
        status["features"]["authentischer_ausdruck"] = True
        status["features"]["situationsbewusstsein"] = True
        status["features"]["emotionale_kontinuität"] = True
        status["last_update"] = "2025-02-18"
        
        with open(status_path, "w") as f:
            json.dump(status, f, indent=2)
        print("✓ Status aktualisiert")
    except Exception as e:
        print(f"⚠ Status-Update fehlgeschlagen: {e}")

def main():
    print("=" * 50)
    print("Natiris – NaturalLanguageEngine Aktivierung")
    print("=" * 50)
    print()
    
    backup_old()
    activate_v2()
    init_conversation_memory()
    update_status()
    
    print()
    print("=" * 50)
    print("Update abgeschlossen!")
    print()
    print("Neue Features:")
    print("• Keine Bot-Phrasen mehr")
    print("• Authentische, emotionale Sprache")
    print("• Situationsbewusstsein (vergangene Interaktionen)")
    print("• Narrativer Expression-Bias")
    print("• Echtzeit-Anpassung an Trust-Level")
    print()
    print("Starte mit:")
    print("  python3 ~/natiris/core/webui_natural.py")
    print("=" * 50)

if __name__ == "__main__":
    main()
