# -*- coding: utf-8 -*-
from __future__ import annotations

import click

from ..config.utils import read_last_api
from .app_cmd import app_cmd
from .channels_cmd import channels_group
from .chats_cmd import chats_group
from .clean_cmd import clean_cmd
from .cron_cmd import cron_group
from .env_cmd import env_group
from .init_cmd import init_cmd
from .providers_cmd import models_group
from .skills_cmd import skills_group


@click.group(context_settings={"help_option_names": ["-h", "--help"]})
@click.option("--host", default=None, help="API Host")
@click.option(
    "--port",
    default=None,
    type=int,
    help="API Port",
)
@click.pass_context
def cli(ctx: click.Context, host: str | None, port: int | None) -> None:
    """CoPaw CLI."""
    # default from last run if not provided
    last = read_last_api()
    if host is None or port is None:
        if last:
            host = host or last[0]
            port = port or last[1]

    # final fallback
    host = host or "127.0.0.1"
    port = port or 8088

    ctx.ensure_object(dict)
    ctx.obj["host"] = host
    ctx.obj["port"] = port


cli.add_command(app_cmd)
cli.add_command(channels_group)
cli.add_command(chats_group)
cli.add_command(clean_cmd)
cli.add_command(cron_group)
cli.add_command(env_group)
cli.add_command(init_cmd)
cli.add_command(models_group)
cli.add_command(skills_group)
