from __future__ import annotations

import os
import sys

from . import __version__
from .config import set_transport_mode
from .security.audit import audit_log
from .server import create_mcp_server


def main() -> None:
    args = sys.argv[1:]

    if "--help" in args or "-h" in args:
        print(
            f"agentvee-mcp v{__version__}\n\n"
            "MCP server for AgentVee — file uploads for AI agents.\n\n"
            "Usage:\n"
            "  agentvee-mcp              Start MCP server (stdio transport)\n"
            "  agentvee-mcp --help       Show this help\n"
            "  agentvee-mcp --version    Show version\n\n"
            "Environment variables:\n"
            "  AGENTVEE_API_KEY          (required) Your AgentVee API key\n"
            "  AGENTVEE_API_BASE_URL     API base URL (default: https://agentvee-api-develop.fly.dev)\n"
        )
        sys.exit(0)

    if "--version" in args or "-v" in args:
        print(__version__)
        sys.exit(0)

    api_key = (os.environ.get("AGENTVEE_API_KEY") or "").strip()
    if not api_key:
        print(
            "[agentvee-mcp] Error: AGENTVEE_API_KEY environment variable is required.\n"
            "Get your API key at https://agentvee.io/dashboard\n",
            file=sys.stderr,
        )
        sys.exit(1)

    set_transport_mode("stdio")
    server = create_mcp_server()
    audit_log({"event": "startup", "transport": "stdio"})
    server.run(transport="stdio")


if __name__ == "__main__":
    main()
