"""ABREKA CLI entry point."""

import click

from abreka.errors import ConfigNotFoundError
from abreka.cli.backlog import backlog
from abreka.cli.branch import branch as branch_group
from abreka.cli.branch.summary import branch_summary_cmd
from abreka.cli.backlog.add import add_cmd as backlog_add_cmd
from abreka.cli.backlog.delete import delete_cmd as backlog_delete_cmd
from abreka.cli.backlog.dismiss import dismiss_cmd as backlog_dismiss_cmd
from abreka.cli.backlog.edit import edit_cmd as backlog_edit_cmd
from abreka.cli.backlog.list_cmd import list_cmd as backlog_list_cmd
from abreka.cli.backlog.prioritize import prioritize_cmd as backlog_prioritize_cmd
from abreka.cli.backlog.resolve import resolve_cmd as backlog_resolve_cmd
from abreka.cli.backlog.show import show_cmd as backlog_show_cmd
from abreka.cli.exp import exp
from abreka.cli.wiki import wiki as wiki_group
from abreka.cli.wiki.add import add_cmd as wiki_add_cmd
from abreka.cli.wiki.append import append_cmd as wiki_append_cmd
from abreka.cli.wiki.delete import delete_cmd as wiki_delete_cmd
from abreka.cli.wiki.edit import edit_cmd as wiki_edit_cmd
from abreka.cli.wiki.list_cmd import list_cmd as wiki_list_cmd
from abreka.cli.wiki.merge import merge_cmd as wiki_merge_cmd
from abreka.cli.wiki.rename import rename_cmd as wiki_rename_cmd
from abreka.cli.wiki.search import search_cmd as wiki_search_cmd
from abreka.cli.wiki.show import show_cmd as wiki_show_cmd
from abreka.cli.wiki.tags import tags_cmd as wiki_tags_cmd
from abreka.cli.exp.new import new_cmd
from abreka.cli.exp.update import update_cmd
from abreka.cli.exp.finish import finish_cmd
from abreka.cli.exp.fail import fail_cmd
from abreka.cli.exp.delete import delete_cmd
from abreka.cli.exp.prune import prune_cmd
from abreka.cli.exp.list_cmd import list_cmd
from abreka.cli.exp.show import show_cmd
from abreka.cli.exp.search import search_cmd
from abreka.cli.exp.diff import diff_cmd
from abreka.cli.exp.tree import tree_cmd
from abreka.cli.exp.chat import chat_cmd as exp_chat_cmd
from abreka.cli.exp.remote import remote_group as exp_remote_group
from abreka.cli.exp.session import session_cmd as exp_session_cmd
from abreka.cli.agent.evaluate import evaluate_cmd
from abreka.cli.agent.implement import implement_cmd
from abreka.cli.agent.propose import propose_cmd
from abreka.cli.agent.test_cmd import test_cmd
from abreka.cli.goal_cmd import goal_cmd
from abreka.cli.chat_cmd import chat_cmd
from abreka.cli.bootstrap_cmd import bootstrap_cmd
from abreka.cli.init_cmd import init_cmd
from abreka.cli.run_cmd import run_cmd
from abreka.cli.status_cmd import status_cmd
from abreka.cli.stop_cmd import stop_cmd
from abreka.cli.util import util
from abreka.cli.util.repair_session_files import repair_session_files_cmd
from abreka.cli.webui import webui


class AbrekaGroup(click.Group):
    """Click group that catches ConfigNotFoundError and shows a friendly message."""

    def invoke(self, ctx):
        try:
            return super().invoke(ctx)
        except ConfigNotFoundError:
            raise click.ClickException(
                "Not inside an abreka project.\n\n"
                "  You might need to:\n"
                "  1. cd into your project directory, or\n"
                "  2. Run 'abreka init .' to set one up.\n"
            )


@click.group(cls=AbrekaGroup)
@click.version_option(package_name="abreka")
@click.option("--debug", is_flag=True, default=False, help="Enable debug logging for pi-rpc.")
@click.pass_context
def main(ctx, debug: bool):
    """ABREKA — Autonomous Research Agent.

    Automates the research experiment loop:
    propose → implement → test → run → evaluate.
    """
    ctx.ensure_object(dict)
    ctx.obj["debug"] = debug


# Top-level commands
main.add_command(bootstrap_cmd, "bootstrap")
main.add_command(init_cmd, "init")
main.add_command(goal_cmd, "goal")
main.add_command(chat_cmd, "chat")
main.add_command(status_cmd, "status")
main.add_command(stop_cmd, "stop")

# Experiment group
exp.add_command(new_cmd, "new")
exp.add_command(update_cmd, "update")
exp.add_command(finish_cmd, "finish")
exp.add_command(fail_cmd, "fail")
exp.add_command(delete_cmd, "delete")
exp.add_command(prune_cmd, "prune")
exp.add_command(list_cmd, "list")
exp.add_command(show_cmd, "show")
exp.add_command(search_cmd, "search")
exp.add_command(diff_cmd, "diff")
exp.add_command(tree_cmd, "tree")
exp.add_command(exp_chat_cmd, "chat")
exp.add_command(exp_remote_group, "remote")
exp.add_command(exp_session_cmd, "session")
main.add_command(exp)

# Backlog group
backlog.add_command(backlog_add_cmd, "add")
backlog.add_command(backlog_edit_cmd, "edit")
backlog.add_command(backlog_list_cmd, "list")
backlog.add_command(backlog_show_cmd, "show")
backlog.add_command(backlog_resolve_cmd, "resolve")
backlog.add_command(backlog_dismiss_cmd, "dismiss")
backlog.add_command(backlog_delete_cmd, "delete")
backlog.add_command(backlog_prioritize_cmd, "prioritize")
main.add_command(backlog)

# Branch group
branch_group.add_command(branch_summary_cmd, "summary")
main.add_command(branch_group, "branch")

# Wiki group
wiki_group.add_command(wiki_add_cmd, "add")
wiki_group.add_command(wiki_append_cmd, "append")
wiki_group.add_command(wiki_delete_cmd, "delete")
wiki_group.add_command(wiki_edit_cmd, "edit")
wiki_group.add_command(wiki_list_cmd, "list")
wiki_group.add_command(wiki_merge_cmd, "merge")
wiki_group.add_command(wiki_rename_cmd, "rename")
wiki_group.add_command(wiki_search_cmd, "search")
wiki_group.add_command(wiki_show_cmd, "show")
wiki_group.add_command(wiki_tags_cmd, "tags")
main.add_command(wiki_group, "wiki")

# Util group
util.add_command(repair_session_files_cmd, "repair-session-files")
main.add_command(util)

# Agent-driven commands (top-level for ergonomics)
main.add_command(propose_cmd, "propose")
main.add_command(implement_cmd, "implement")
main.add_command(test_cmd, "test")
main.add_command(evaluate_cmd, "evaluate")

# Orchestration
main.add_command(run_cmd, "run")

# Web UI
main.add_command(webui)
