#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MLXMate - Your MLX-powered coding companion
Global executable for npm package
"""

import os
import sys
import subprocess
from pathlib import Path

def get_python_executable():
    """Get the appropriate Python executable"""
    # Use the system Python executable
    return sys.executable

def get_package_root():
    """Get the root directory of the installed package"""
    # Get the directory where this script is located
    script_dir = Path(__file__).parent.parent.absolute()
    
    # If this is a global npm installation, the structure is different
    if script_dir.name == 'bin':
        # We're in /usr/local/bin or /opt/homebrew/bin, so go up to lib/node_modules/mlxmate
        return script_dir.parent / 'lib' / 'node_modules' / 'mlxmate'
    elif script_dir.name == 'node_modules':
        # We're in node_modules, so the package is the next directory
        return script_dir / 'mlxmate'
    elif script_dir.name == 'homebrew':
        # We're in /opt/homebrew, so go to lib/node_modules/mlxmate
        return script_dir / 'lib' / 'node_modules' / 'mlxmate'
    
    return script_dir

def main():
    """Main entry point"""
    # Get the package root directory
    package_root = get_package_root()
    
    # Add the package root to Python path
    sys.path.insert(0, str(package_root))
    
    # Check if we have command line arguments
    if len(sys.argv) > 1:
        command = sys.argv[1]
        args = sys.argv[2:]
        
        if command == "interactive":
            # Run interactive mode from package directory
            script_path = package_root / "interactive_mlx.py"
            subprocess.run([get_python_executable(), str(script_path)])
        elif command == "test":
            # Run test from package directory
            script_path = package_root / "test_mlx.py"
            subprocess.run([get_python_executable(), str(script_path)])
        elif command == "list-files":
            # Show files being indexed from current directory
            import os as os_module
            current_dir = os_module.getcwd()
            script_path = package_root / "list_indexed_files.py"
            subprocess.run([get_python_executable(), str(script_path)], cwd=current_dir)
        elif command == "setup":
            # Run setup from package directory
            script_path = package_root / "terminal_claude.py"
            subprocess.run([get_python_executable(), str(script_path), "setup"])
        elif command == "help":
            # Show help
            print("MLXMate - Your MLX-powered coding companion")
            print("\nUsage:")
            print("  mlxmate                            # Start interactive mode")
            print("  mlxmate interactive                # Start interactive chat")
            print("  mlxmate test                       # Test MLX integration")
            print("  mlxmate setup                      # Run initial setup")
            print("  mlxmate help                       # Show this help")
            print("\nCommands:")
            print("  mlxmate list-files                 # Show files being indexed")
            print("  mlxmate search <query>             # Search codebase")
            print("  mlxmate analyze <file>             # Analyze a file")
            print("  mlxmate generate <prompt>          # Generate code")
            print("  mlxmate review <file>              # Review code")
            print("  mlxmate refactor <file>            # Refactor code")
        else:
            # Pass through to the main terminal_claude.py from package directory
            script_path = package_root / "terminal_claude.py"
            subprocess.run([get_python_executable(), str(script_path)] + sys.argv[1:])
    else:
        # No arguments, start interactive mode from package directory
        script_path = package_root / "interactive_mlx.py"
        subprocess.run([get_python_executable(), str(script_path)])

if __name__ == "__main__":
    main()
