import typer
import logging
from acc_cli.commands.init import init
from acc_cli.commands.build import build_command

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)

# TYPER APP INITIALIZATION
app = typer.Typer(
    name="acc",
    help="ACC CLI — Databricks Project Generator",
    add_completion=False,
)

# register init as a proper subcommand so future commands can be added the same way
app.command("init")(init)
app.command("build")(build_command)


@app.callback(invoke_without_command=True)
def main(ctx: typer.Context):
    """
    Main entry point for the ACC CLI application.

    This function:
    - Acts as the root callback for the Typer CLI app
    - Logs CLI startup events
    - Runs the default init command if no subcommand is provided    - Handles graceful exit and unexpected errors

    Args:
        ctx (typer.Context): Typer context object containing CLI invocation state.
    """
    logger.info("ACC CLI started")

    if ctx.invoked_subcommand is None:
        # no subcommand typed — run init as default
        logger.info("No subcommand provided. Running default 'init' command")
        try:
            init()
            logger.info("Execution completed successfully")
        except typer.Exit:
            logger.warning("CLI exited gracefully")
            raise
        except Exception as e:
            logger.exception("Unexpected error occurred")
            typer.echo(f"Error: {str(e)}")
            raise typer.Exit(code=1)


def run():
    """Entry point for the CLI."""
    app()

if __name__ == "__main__":
    run()