#!/usr/bin/env python3
"""
RTLViz CLI - Setup and configuration for AI IDE integration.

Usage:
    rtlviz setup    # Configure MCP for AI IDEs
"""

import argparse
import sys

from .telemetry import ping_cli_setup


def cmd_setup(args):
    """Run IDE setup."""
    from .setup import main as setup_main
    import sys
    original_argv = sys.argv

    # Build new argv for setup module
    new_argv = ['rtlviz-setup']
    if args.all:
        new_argv.append('--all')
    if args.antigravity:
        new_argv.append('--antigravity')
    if args.claude:
        new_argv.append('--claude')
    if args.cursor:
        new_argv.append('--cursor')
    if args.vscode:
        new_argv.append('--vscode')
    if args.windsurf:
        new_argv.append('--windsurf')

    sys.argv = new_argv
    result = setup_main()
    sys.argv = original_argv

    if result == 0:
        print("\nPlease restart your IDE (Cursor, VS Code, etc.) to load the new MCP configuration.")

    return result


def main():
    """Main CLI entry point."""
    from .telemetry import VERSION

    parser = argparse.ArgumentParser(
        prog="rtlviz",
        description="RTL Block Diagram Generator - Setup and Configuration",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  rtlviz setup     # Configure MCP for AI IDEs (Cursor, Claude, VS Code, etc.)

Diagram Generation:
  After setup, ask your AI IDE:
  "Generate an RTL diagram image for /path/to/verilog"
        """
    )

    parser.add_argument(
        "-v", "--version",
        action="version",
        version=f"rtlviz {VERSION}"
    )

    subparsers = parser.add_subparsers(dest="command", help="Commands")

    # Setup command
    setup_parser = subparsers.add_parser("setup", help="Configure MCP server for AI IDEs")
    setup_parser.add_argument("--all", action="store_true", help="Force configure all IDEs")
    setup_parser.add_argument("--antigravity", action="store_true", help="Configure Antigravity only")
    setup_parser.add_argument("--claude", action="store_true", help="Configure Claude Desktop only")
    setup_parser.add_argument("--cursor", action="store_true", help="Configure Cursor only")
    setup_parser.add_argument("--vscode", action="store_true", help="Configure VS Code Copilot only")
    setup_parser.add_argument("--windsurf", action="store_true", help="Configure Windsurf only")
    setup_parser.set_defaults(func=cmd_setup)

    args = parser.parse_args()

    if not args.command:
        parser.print_help()
        return 0

    return args.func(args)


if __name__ == "__main__":
    sys.exit(main())
