import logging
import sys
from argh import ArghParser
from scale_egp.cli.collections import (
    CollectionCRUDCommands,
    EGPClientFactory,
    ModelAliasCommands,
    ModelTemplateCommands,
    UserCommands,
)
from scale_egp.cli.formatter import AVAILABLE_FORMATTERS
from scale_egp.cli.parser import get_parser, dispatch


def add_commands_to_parser(parser: ArghParser, commands: CollectionCRUDCommands):
    parser.add_commands(
        [
            getattr(commands, key)
            for key in dir(commands)
            if not key.startswith("_") and callable(getattr(commands, key))
        ],
        group_name=commands.command_group_name,
    )


def exec_cli():
    client_factory = EGPClientFactory()

    parser = get_parser()
    parser.add_argument("--log-curl-commands", action="store_true", default=False)
    parser.add_argument("-a", "--api-key", type=str, default=None)
    parser.add_argument(
        "-i",
        "--account-id",
        type=str,
        default=None,
        help="Optional EGP account_id to use. The default account_id will be used if not set.",
    )
    parser.add_argument("-e", "--endpoint-url", type=str, default=None)
    parser.add_argument(
        "-f", "--format", type=str, choices=[*AVAILABLE_FORMATTERS.keys()], default="rich"
    )
    add_commands_to_parser(parser, ModelAliasCommands(client_factory))
    add_commands_to_parser(parser, ModelTemplateCommands(client_factory))
    add_commands_to_parser(parser, UserCommands(client_factory))
    args = parser.parse_args()
    try:
        client_factory.set_client_kwargs(
            api_key=args.api_key,
            endpoint_url=args.endpoint_url,
            account_id=args.account_id,
            log_curl_commands=args.log_curl_commands,
        )
    except Exception as e:
        print(f"ERROR: {str(e)}")
        sys.exit(1)
    if args.log_curl_commands:
        logging.basicConfig(level=logging.INFO)
    dispatch(parser, args)
