"""Three Blocks — the hub addon (docs/plans/three-blocks-blender-addon.md).

The one Blender addon a user installs. It shares the sign-in credential with
the `three-blocks` CLI and owns the catalog, install, and runtime lifecycle for
managed tool-addons.

Ships with BOTH manifests: blender_manifest.toml makes it a 4.2+ extension,
this bl_info keeps legacy (pre-4.2) scripts/addons installs working.
"""

bl_info = {
    "name": "Three Blocks",
    "author": "Utsubo",
    "version": (0, 3, 3),
    "blender": (3, 6, 0),
    "location": "View3D > Sidebar > Three Blocks",
    "description": "Manage Three Blocks tools — installed tools stay local",
    "category": "3D View",
}

import sys

from . import auth, manager, ops, panels, prefs
from .context import HUB_API, get_context  # re-export: the hub↔tool contract

__all__ = ["HUB_API", "get_context", "register", "unregister"]


def _alias_module():
    """Make `import three_blocks` resolve to this package everywhere.

    Tool-addons address the hub by its documented name (`three_blocks` —
    see utsubo_utsbv/encode.py). Installed as a 4.2+ extension this package is
    actually `bl_ext.<repo>.three_blocks`, so the plain name would not exist;
    alias it, and only clean up an alias that still points at us.
    """
    sys.modules.setdefault("three_blocks", sys.modules[__name__])


def _unalias_module():
    if sys.modules.get("three_blocks") is sys.modules[__name__]:
        if __name__ != "three_blocks":
            del sys.modules["three_blocks"]


def register():
    _alias_module()
    prefs.register()
    auth.register()
    ops.register()
    panels.register()
    manager.cleanup_staging()
    manager.register_installed()
    panels.invalidate_status()
    # Startup is fully local. Account status and catalog updates are fetched
    # only after sign-in or an explicit Refresh action.


def unregister():
    manager.unregister_all()
    panels.unregister()
    ops.unregister()
    auth.unregister()
    prefs.unregister()
    _unalias_module()


if __name__ == "__main__":
    register()
