"""Main entry point for NoteFlow CLI.

Usage:
    python -m noteflow.cli retention [cleanup|status]
    python -m noteflow.cli models [download|list|status]
"""

import sys

from rich.console import Console

from noteflow.infrastructure.logging import get_logger

console = Console()
logger = get_logger(__name__)


def main() -> None:
    """Dispatch to appropriate subcommand CLI."""
    logger.info("cli_invoked", argv=sys.argv)

    if len(sys.argv) < 2:
        logger.debug("cli_no_command", message="No command provided, showing help")
        console.print("[bold]NoteFlow CLI[/bold]")
        console.print()
        console.print("Available commands:")
        console.print("  retention  - Meeting retention management")
        console.print("  models     - ML model download management")
        console.print()
        console.print("Usage:")
        console.print("  python -m noteflow.cli <command> [options]")
        console.print()
        console.print("Examples:")
        console.print("  python -m noteflow.cli retention status")
        console.print("  python -m noteflow.cli retention cleanup --dry-run")
        console.print("  python -m noteflow.cli models list")
        console.print("  python -m noteflow.cli models download")
        sys.exit(1)

    command = sys.argv[1]
    subcommand_args = sys.argv[2:]

    # Remove the command from argv so submodule parsers work correctly
    sys.argv = [sys.argv[0], *subcommand_args]

    if command == "retention":
        logger.debug("cli_dispatch", command=command, subcommand_args=subcommand_args)
        try:
            from noteflow.cli.retention import main as retention_main

            retention_main()
        except Exception:
            logger.exception("cli_command_failed", command=command)
            raise
    elif command == "models":
        logger.debug("cli_dispatch", command=command, subcommand_args=subcommand_args)
        try:
            from noteflow.cli.models import main as models_main

            models_main()
        except Exception:
            logger.exception("cli_command_failed", command=command)
            raise
    else:
        logger.warning("cli_unknown_command", command=command)
        console.print(f"[red]Unknown command:[/red] {command}")
        console.print("Available commands: retention, models")
        sys.exit(1)


if __name__ == "__main__":
    main()
