"""Flow CLI - Clean orchestrator for command modules."""

import click

# Backward compatibility for tests that mock Flow in app.py
from flow import Flow  # noqa: F401 - Re-exported for test compatibility
from flow.cli.commands import get_commands


@click.group()
@click.version_option(version="2.0.0", prog_name="flow")
def cli():
    """Flow CLI - Submit and manage GPU tasks.

    Examples:
        flow run training.yaml
        flow status
        flow logs task_123456
    """
    pass


def setup_cli():
    """Set up the CLI by registering all commands."""
    # Get all command instances
    commands = get_commands()

    # Register each command with the CLI
    for command in commands:
        cmd = command.get_command()

        # Handle both regular commands and command groups
        if isinstance(cmd, (click.Command, click.Group)):
            cli.add_command(cmd)
        else:
            raise TypeError(f"Command {command.name} must return a click.Command or click.Group")

    return cli


# Set up the CLI
cli = setup_cli()

# Export main for backward compatibility
def main():
    """Main entry point for CLI."""
    return cli()


if __name__ == "__main__":
    cli()
