#!/usr/bin/env python3
"""Debug-Version zum Testen des Backends"""

import subprocess
import os
from pathlib import Path

def test_backend():
    base_dir = Path(__file__).parent.absolute()
    backend_dir = base_dir / "backend"
    
    print(f"Backend-Verzeichnis: {backend_dir}")
    print(f"Existiert: {backend_dir.exists()}")
    
    if not backend_dir.exists():
        print("❌ Backend-Verzeichnis nicht gefunden!")
        return
        
    # Prüfe package.json
    package_json = backend_dir / "package.json"
    if not package_json.exists():
        print("❌ package.json nicht gefunden!")
        return
        
    print("\n📁 Backend-Inhalt:")
    for item in backend_dir.iterdir():
        print(f"   - {item.name}")
        
    # Prüfe node_modules
    node_modules = backend_dir / "node_modules"
    if not node_modules.exists():
        print("\n⚠️  node_modules nicht gefunden! Installiere Dependencies...")
        subprocess.run(["npm", "install"], cwd=backend_dir, shell=True)
        
    print("\n🚀 Starte Backend...")
    
    # Starte Backend mit sichtbarer Ausgabe
    process = subprocess.Popen(
        "npm run dev",
        cwd=backend_dir,
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        universal_newlines=True,
        bufsize=1
    )
    
    # Zeige Live-Ausgabe
    try:
        for line in process.stdout:
            print(f"   {line}", end='')
    except KeyboardInterrupt:
        process.terminate()
        print("\n\n🛑 Backend gestoppt.")

if __name__ == "__main__":
    print("🔍 SkillMate Backend Debug\n")
    test_backend()