"""CLI app definition and initialization."""

from pathlib import Path
from typing import Annotated

import typer
from mm_clikit import CoreContext, TyperPlus, setup_logging

from mb_stash.cli.commands.add import add
from mb_stash.cli.commands.change_password import change_password
from mb_stash.cli.commands.daemon import daemon
from mb_stash.cli.commands.delete import delete
from mb_stash.cli.commands.get import get
from mb_stash.cli.commands.health import health
from mb_stash.cli.commands.init import init
from mb_stash.cli.commands.list import list_
from mb_stash.cli.commands.lock import lock
from mb_stash.cli.commands.rename import rename
from mb_stash.cli.commands.stop import stop
from mb_stash.cli.commands.unlock import unlock
from mb_stash.cli.output import Output
from mb_stash.config import Config
from mb_stash.core import Core

app = TyperPlus(package_name="mb-stash")


@app.callback()
def main(
    ctx: typer.Context,
    *,
    data_dir: Annotated[Path | None, typer.Option("--data-dir", help="Data directory. Env: MB_STASH_DATA_DIR.")] = None,
) -> None:
    """Quick access to non-critical secrets from the terminal."""
    config = Config.build(data_dir)
    setup_logging("mb_stash", config.log_path)
    core = Core(config)
    ctx.call_on_close(core.close)
    ctx.obj = CoreContext(core=core, out=Output())


# Setup
app.command()(init)
app.command("change-password")(change_password)

# Daemon
app.command(hidden=True)(daemon)
app.command()(stop)
app.command()(lock)
app.command()(unlock)
app.command(aliases=["h"])(health)

# Secrets
app.command(aliases=["g"])(get)
app.command("list", aliases=["l"])(list_)
app.command()(add)
app.command()(delete)
app.command()(rename)
