#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2025 - Present Sepine Tam, Inc. All Rights Reserved
#
# @Author : Sepine Tam (谭淞)
# @Email  : sepinetam@gmail.com
# @File   : _cli.py

from __future__ import annotations

import sys

from ._handlers import (
    handle_agent,
    handle_config,
    handle_install,
    handle_sandbox,
    handle_server,
    handle_tool,
    handle_usable,
)
from ._parsers import (
    add_agent_parser,
    add_config_parser,
    add_install_parser,
    add_sandbox_parser,
    add_tool_parser,
    create_root_parser,
)


def main() -> None:
    """Entry point for the command line interface."""
    parser = create_root_parser()
    subparsers = parser.add_subparsers(dest="command", help="Available commands")

    agent_parser = add_agent_parser(subparsers)
    tool_parser = add_tool_parser(subparsers)
    config_parser = add_config_parser(subparsers)
    add_install_parser(subparsers)
    add_sandbox_parser(subparsers)

    args = parser.parse_args()

    if args.usable:
        sys.exit(handle_usable())

    if args.command == "agent":
        if args.agent_action == "run":
            handle_agent(args)
            return
        agent_parser.print_help()
        return

    if args.command == "tool":
        exit_code = handle_tool(args)
        if exit_code == 2:
            tool_parser.print_help()
        sys.exit(exit_code)

    if args.command == "config":
        exit_code = handle_config(args)
        if exit_code != 0:
            config_parser.print_help()
        sys.exit(exit_code)

    if args.command == "install":
        sys.exit(handle_install(args))

    if args.command == "sandbox-install":
        sys.exit(handle_sandbox(args))

    handle_server(args)
