#!/usr/bin/env python3
"""
Basic test for NexusBackend implementation.

This test verifies that the NexusBackend correctly wraps Nexus operations
without requiring deepagents to be installed.
"""

import shutil
import sys
import tempfile
from pathlib import Path

# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))

import nexus


async def test_basic_operations():
    """Test basic file operations through NexusBackend."""

    print("Testing NexusBackend basic operations...")
    print("=" * 70)
    print()

    # Create temporary directory for test
    test_dir = tempfile.mkdtemp(prefix="nexus_backend_test_")
    print(f"Test directory: {test_dir}")
    print()

    try:
        # Connect to Nexus (default standalone mode)
        nx = nexus.connect()
        print("✓ Connected to Nexus (standalone mode)")

        # Note: We can't import NexusBackend without deepagents installed
        # So we'll test the underlying Nexus operations directly
        print()
        print("Testing underlying Nexus operations:")
        print("-" * 70)

        # Test write
        print("\n1. Testing write operation...")
        test_path = "/test/file.txt"
        content = "Hello, Nexus!"
        nx.sys_write(test_path, content.encode("utf-8"))
        print(f"   ✓ Wrote to {test_path}")

        # Test read
        print("\n2. Testing read operation...")
        read_content = nx.sys_read(test_path).decode("utf-8")
        assert read_content == content, f"Content mismatch: {read_content} != {content}"
        print(f"   ✓ Read from {test_path}: {read_content}")

        # Test list
        print("\n3. Testing list operation...")
        files = nx.sys_readdir("/test")
        assert any("file.txt" in f for f in files), f"file.txt not found in {files}"
        print(f"   ✓ Listed /test: {len(files)} files")

        # Test exists
        print("\n4. Testing exists operation...")
        assert await nx.access(test_path), f"{test_path} should exist"
        print(f"   ✓ File exists: {test_path}")

        # Test versioning (edit simulation)
        print("\n5. Testing versioning (multiple writes)...")
        for i in range(3):
            new_content = f"Version {i + 1}"
            nx.sys_write(test_path, new_content.encode("utf-8"))
            print(f"   ✓ Wrote version {i + 1}")

        from nexus.lib.sync_bridge import run_sync

        versions = run_sync(nx.version_service.list_versions(test_path))
        print(f"   ✓ File has {len(versions)} versions")
        assert len(versions) >= 4, f"Expected at least 4 versions, got {len(versions)}"

        # Test glob
        print("\n6. Testing glob operation...")
        nx.sys_write("/test/file1.txt", b"content1")
        nx.sys_write("/test/file2.md", b"content2")
        matches = nx.glob("/test/*.txt")
        print(f"   ✓ Glob /test/*.txt: {len(matches)} matches")
        assert len(matches) == 2, f"Expected 2 matches, got {len(matches)}"

        # Test grep (manual since NexusBackend does this)
        print("\n7. Testing content search (grep simulation)...")
        nx.sys_write("/test/searchable.txt", b"Hello World\nFoo Bar\nHello Again")
        content_bytes = nx.sys_read("/test/searchable.txt")
        content_str = content_bytes.decode("utf-8")
        lines_with_hello = [
            (i + 1, line) for i, line in enumerate(content_str.splitlines()) if "Hello" in line
        ]
        print(f"   ✓ Found 'Hello' in {len(lines_with_hello)} lines")
        assert len(lines_with_hello) == 2

        print()
        print("=" * 70)
        print("✅ All basic operations work!")
        print()
        print("Next steps:")
        print("  1. Install deepagents: pip install deepagents")
        print("  2. Set ANTHROPIC_API_KEY environment variable")
        print("  3. Set TAVILY_API_KEY environment variable (optional)")
        print("  4. Run: python research/demo_1_drop_in.py")
        print()

    finally:
        # Cleanup
        shutil.rmtree(test_dir)
        print("✓ Cleaned up test directory")


def test_backend_interface():
    """Test that NexusBackend matches expected interface."""

    print()
    print("=" * 70)
    print("Testing NexusBackend Protocol Compliance")
    print("=" * 70)
    print()

    try:
        from nexus_backend import NexusBackend

        print("✓ NexusBackend imports successfully")
        print()

        # Check required methods exist
        required_methods = ["ls_info", "read", "write", "edit", "glob_info", "grep_raw"]

        for method in required_methods:
            assert hasattr(NexusBackend, method), f"Missing method: {method}"
            print(f"  ✓ {method} method exists")

        print()
        print("✅ NexusBackend implements all required methods!")
        print()

    except ImportError:
        print("⚠️  deepagents not installed")
        print("   Install with: pip install deepagents")
        print()
        print("   The NexusBackend implementation is complete, but cannot be")
        print("   fully tested without deepagents installed.")
        print()


if __name__ == "__main__":
    # Test basic Nexus operations
    test_basic_operations()

    # Test backend interface (if deepagents available)
    test_backend_interface()
