#!/usr/bin/env python3
"""HELIX installer for Databricks Genie Code.

Downloads the HELIX skill bundle from GitHub and uploads it to a
Databricks workspace. By default installs to the current user
(`/Users/<you>/.assistant/skills/helix/`); use `--shared` for the
workspace-wide path (requires workspace admin). By default pulls the
latest release tag; use `--main` to pull from the main branch or
`--version vX.Y.Z` to pin a specific tag.

Usage:
    genie-install                            # latest release, current user
    genie-install --shared                   # latest release, workspace-wide
    genie-install --main                     # main branch, current user
    genie-install --version v0.3.4           # specific release
    genie-install --bundle /path/to/helix    # local bundle (skip download)
    genie-install --target /Workspace/...    # custom workspace path

Auth: the Databricks SDK auto-detects credentials in this order:
  1. Inside a Databricks notebook: implicit workspace context (no env
     vars required); the SDK uses the notebook's identity.
  2. `DATABRICKS_HOST` + `DATABRICKS_TOKEN` env vars.
  3. `DATABRICKS_PROFILE` env var naming a section in
     `~/.databrickscfg` (managed by `databricks configure`).
  4. Default profile in `~/.databrickscfg` (if present).

You do NOT need the Databricks CLI installed. The script speaks
directly to the Databricks REST API via the SDK.

Runtime: plain `python3` (>= 3.8). No `uv` required. Installs the
`databricks-sdk` and `PyYAML` packages on first run if they aren't
already present — works inside a Databricks notebook cell, where
both pip and Databricks credentials are pre-configured, or any dev
environment with pip on PATH.

Exit codes:
    0  install + verify succeeded
    2  missing or invalid env / args / unavailable deps
    3  bundle source unavailable
    4  upload error
    5  verify failed (install incomplete)
"""

from __future__ import annotations

# --- self-bootstrapping deps ---------------------------------------------
# Done BEFORE non-stdlib imports so the script runs out of the box on a
# fresh Databricks notebook or a dev box without manual setup.

import importlib
import subprocess
import sys


_REQS = [
    ("databricks.sdk", "databricks-sdk>=0.30"),
    ("yaml", "PyYAML>=6"),
]


def _ensure_deps() -> None:
    missing_pkgs: list[str] = []
    for mod, pkg in _REQS:
        try:
            importlib.import_module(mod)
        except ImportError:
            missing_pkgs.append(pkg)
    if not missing_pkgs:
        return

    print(
        f"installing missing dependencies via pip: {' '.join(missing_pkgs)}",
        file=sys.stderr,
    )
    # Try several pip invocations in order of preference. Stop on first success.
    attempts = [
        [sys.executable, "-m", "pip", "install", "--quiet", *missing_pkgs],
        [sys.executable, "-m", "pip", "install", "--quiet", "--user", *missing_pkgs],
        # PEP 668: externally-managed Pythons (some Linux distros) require this.
        [
            sys.executable,
            "-m",
            "pip",
            "install",
            "--quiet",
            "--break-system-packages",
            *missing_pkgs,
        ],
    ]
    for cmd in attempts:
        try:
            subprocess.check_call(
                cmd,
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
            )
            importlib.invalidate_caches()
            break
        except (subprocess.CalledProcessError, FileNotFoundError):
            continue
    else:
        print(
            "\nerror: could not pip-install the required dependencies.\n"
            f"Install them manually then re-run:\n"
            f"    pip install {' '.join(missing_pkgs)}\n",
            file=sys.stderr,
        )
        sys.exit(2)

    # Verify the imports work now.
    for mod, pkg in _REQS:
        try:
            importlib.import_module(mod)
        except ImportError as exc:
            print(
                f"error: {pkg} installed but cannot import {mod}: {exc}",
                file=sys.stderr,
            )
            sys.exit(2)


_ensure_deps()


# --- now safe to import everything --------------------------------------

import argparse
import shutil
import tarfile
import tempfile
import urllib.error
import urllib.request
from pathlib import Path


DEFAULT_REPO = "easel/helix"
WORKSPACE_TARGET = "/Workspace/.assistant/skills/helix"
SKILL_NAME = "helix"
EXPECTED_ACTIVITIES = [
    "00-discover",
    "01-frame",
    "02-design",
    "03-test",
    "04-build",
    "05-deploy",
    "06-iterate",
]


def fail(code: int, msg: str) -> None:
    print(f"error: {msg}", file=sys.stderr)
    sys.exit(code)


def info(msg: str) -> None:
    print(f"  {msg}", flush=True)


def parse_args() -> argparse.Namespace:
    p = argparse.ArgumentParser(
        description=__doc__.strip().split("\n")[0],
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=__doc__,
    )
    src = p.add_mutually_exclusive_group()
    src.add_argument(
        "--main",
        action="store_true",
        help="install from main branch HEAD instead of latest release",
    )
    src.add_argument(
        "--version", metavar="vX.Y.Z", help="install a specific release tag"
    )
    src.add_argument(
        "--bundle",
        type=Path,
        help="install from a local pre-built bundle directory (skip download)",
    )

    target = p.add_mutually_exclusive_group()
    target.add_argument(
        "--shared",
        action="store_true",
        help="install workspace-wide at /Workspace/.assistant/skills/helix (requires admin)",
    )
    target.add_argument("--target", help="explicit workspace path (overrides --shared)")

    p.add_argument(
        "--repo",
        default=DEFAULT_REPO,
        help=f"GitHub repo to install from (default: {DEFAULT_REPO})",
    )
    p.add_argument(
        "--keep-temp",
        action="store_true",
        help="don't delete the temp directory after install (for debugging)",
    )
    return p.parse_args()


# --- bundle acquisition ---------------------------------------------------


def fetch_url(url: str, dest: Path) -> None:
    info(f"fetching {url}")
    req = urllib.request.Request(url, headers={"User-Agent": "helix-genie-install/1"})
    try:
        with urllib.request.urlopen(req, timeout=60) as resp, open(dest, "wb") as out:
            shutil.copyfileobj(resp, out)
    except urllib.error.HTTPError as exc:
        fail(3, f"HTTP {exc.code} fetching {url}")
    except urllib.error.URLError as exc:
        fail(3, f"URL error fetching {url}: {exc.reason}")


def resolve_latest_tag(repo: str) -> str:
    api = f"https://api.github.com/repos/{repo}/releases/latest"
    info(f"resolving latest release from {api}")
    try:
        req = urllib.request.Request(
            api, headers={"User-Agent": "helix-genie-install/1"}
        )
        with urllib.request.urlopen(req, timeout=20) as resp:
            import json

            data = json.load(resp)
            tag = data.get("tag_name")
            if not tag:
                fail(3, f"no tag_name in {api} response")
            return tag
    except urllib.error.HTTPError as exc:
        if exc.code == 404:
            fail(3, f"no releases yet on {repo}. Use --main or --version <tag>.")
        fail(3, f"HTTP {exc.code} resolving latest release on {repo}")
    except urllib.error.URLError as exc:
        fail(3, f"can't reach GitHub API: {exc.reason}")


def acquire_bundle(args: argparse.Namespace, workdir: Path) -> Path:
    """Return a path to a built bundle directory ready for upload."""
    if args.bundle:
        if not args.bundle.is_dir():
            fail(3, f"--bundle path is not a directory: {args.bundle}")
        return args.bundle.resolve()

    if args.main:
        ref = "main"
        ref_kind = "branch"
    elif args.version:
        ref = args.version
        ref_kind = "tag"
    else:
        ref = resolve_latest_tag(args.repo)
        ref_kind = "tag"
        info(f"latest release: {ref}")

    # Prefer a pre-built bundle release asset.
    if ref_kind == "tag":
        asset_url = (
            f"https://github.com/{args.repo}/releases/download/{ref}/"
            f"helix-genie-bundle-{ref}.tar.gz"
        )
        try:
            req = urllib.request.Request(
                asset_url,
                method="HEAD",
                headers={"User-Agent": "helix-genie-install/1"},
            )
            urllib.request.urlopen(req, timeout=10).close()
            info(f"pre-built bundle exists at {asset_url}")
            bundle_tgz = workdir / "bundle.tar.gz"
            fetch_url(asset_url, bundle_tgz)
            extract_dir = workdir / "bundle"
            extract_dir.mkdir()
            with tarfile.open(bundle_tgz) as tf:
                tf.extractall(extract_dir)
            candidate = extract_dir / "helix"
            if candidate.is_dir():
                return candidate
            for child in extract_dir.iterdir():
                if child.is_dir() and (child / "SKILL.md").is_file():
                    return child
            fail(3, f"bundle tarball has unexpected layout under {extract_dir}")
        except urllib.error.HTTPError as exc:
            if exc.code != 404:
                fail(3, f"HTTP {exc.code} checking {asset_url}")
            info(f"no pre-built bundle at {ref}; will build from source")
        except urllib.error.URLError as exc:
            info(f"can't reach release asset ({exc.reason}); will build from source")

    # Fallback: download source archive and run scripts/build-genie-bundle.sh.
    src_url = f"https://github.com/{args.repo}/archive/refs/{'heads' if ref_kind == 'branch' else 'tags'}/{ref}.tar.gz"
    src_tgz = workdir / "source.tar.gz"
    fetch_url(src_url, src_tgz)

    src_dir = workdir / "src"
    src_dir.mkdir()
    info("extracting source")
    with tarfile.open(src_tgz) as tf:
        tf.extractall(src_dir)
    roots = [p for p in src_dir.iterdir() if p.is_dir()]
    if not roots:
        fail(3, f"source archive has no top-level directory at {src_dir}")
    repo_root = roots[0]

    builder = repo_root / "scripts" / "build-genie-bundle.sh"
    if not builder.is_file():
        fail(3, f"build-genie-bundle.sh not found in source at {builder}")

    info("running build-genie-bundle.sh")
    out_dir = workdir / "build-out"
    try:
        subprocess.run(
            ["bash", str(builder), "--out", str(out_dir)],
            cwd=str(repo_root),
            check=True,
        )
    except subprocess.CalledProcessError as exc:
        fail(3, f"build-genie-bundle.sh failed: exit {exc.returncode}")

    bundle = out_dir / "helix"
    if not (bundle / "SKILL.md").is_file():
        fail(3, f"build produced no SKILL.md at {bundle}")
    return bundle


# --- bundle validation ----------------------------------------------------


def validate_bundle(bundle: Path) -> None:
    if bundle.name != SKILL_NAME:
        fail(
            3,
            f"bundle parent dir is '{bundle.name}', must be '{SKILL_NAME}' "
            f"(agentskills.io spec invariant)",
        )
    skill_md = bundle / "SKILL.md"
    if not skill_md.is_file():
        fail(3, f"bundle missing SKILL.md at {skill_md}")
    content = skill_md.read_text()
    if not content.startswith("---\n"):
        fail(3, "SKILL.md has no YAML frontmatter")
    info(f"bundle OK ({bundle})")


# --- auth + upload --------------------------------------------------------


def make_client():
    """Construct a WorkspaceClient using whatever auth the environment offers.

    Order of preference (handled by the SDK):
      - Inside a Databricks notebook: implicit workspace identity.
      - DATABRICKS_HOST + DATABRICKS_TOKEN env vars.
      - DATABRICKS_PROFILE env var → ~/.databrickscfg section.
      - DEFAULT profile in ~/.databrickscfg.

    No Databricks CLI required; the SDK reads the config file directly.
    """
    from databricks.sdk import WorkspaceClient

    try:
        w = WorkspaceClient()
        # Force an auth round-trip so failures surface here, not later.
        w.current_user.me()
        return w
    except Exception as exc:
        fail(
            2,
            "Databricks workspace auth failed.\n"
            f"  {exc}\n"
            "Inside a Databricks notebook this should not happen. "
            "Outside a notebook, set either:\n"
            "    DATABRICKS_HOST=https://<workspace>.databricks.com\n"
            "    DATABRICKS_TOKEN=<personal access token>\n"
            "or:\n"
            "    DATABRICKS_PROFILE=<section in ~/.databrickscfg>\n"
            "and re-run.",
        )


def grant_users_read(client, path: str) -> bool:
    """Ensure the `users` group has CAN_READ on a workspace directory.

    Required for `--shared` installs: in workspaces with a tight root ACL
    (only `admins` granted at `/directories/`), every object created under
    `/Workspace/.assistant/...` inherits admin-only — so non-admin users
    can't see the skill and Genie won't surface it. This function
    idempotently adds `users: CAN_READ` if absent.

    Returns True if the grant is in place (already or after update),
    False on a non-fatal error (logged as a warning). Non-fatal because
    a user lacking admin can still complete a `--shared` install in a
    workspace whose root ACL is already permissive; we just couldn't
    confirm.
    """
    try:
        from databricks.sdk.service.workspace import (
            WorkspaceObjectAccessControlRequest,
            WorkspaceObjectPermissionLevel,
        )
    except ImportError as exc:
        info(
            f"warn: ACL types unavailable in this SDK version ({exc}); skipping ACL grant"
        )
        return False

    try:
        status = client.workspace.get_status(path)
        obj_id = str(status.object_id)
    except Exception as exc:
        info(f"warn: get-status {path}: {exc}; skipping ACL grant")
        return False

    grant_levels = {
        WorkspaceObjectPermissionLevel.CAN_READ,
        WorkspaceObjectPermissionLevel.CAN_RUN,
        WorkspaceObjectPermissionLevel.CAN_EDIT,
        WorkspaceObjectPermissionLevel.CAN_MANAGE,
    }
    try:
        perms = client.workspace.get_permissions(
            workspace_object_type="directories",
            workspace_object_id=obj_id,
        )
        for acl in perms.access_control_list or []:
            if acl.group_name != "users":
                continue
            for p in acl.all_permissions or []:
                if p.permission_level in grant_levels:
                    info(
                        f"ACL OK on {path}: users already has {p.permission_level.value}"
                    )
                    return True
    except Exception as exc:
        info(f"warn: get-permissions {path}: {exc}; will attempt grant anyway")

    try:
        client.workspace.update_permissions(
            workspace_object_type="directories",
            workspace_object_id=obj_id,
            access_control_list=[
                WorkspaceObjectAccessControlRequest(
                    group_name="users",
                    permission_level=WorkspaceObjectPermissionLevel.CAN_READ,
                ),
            ],
        )
        info(f"granted users CAN_READ on {path}")
        return True
    except Exception as exc:
        info(f"warn: update-permissions {path}: {exc}")
        info("non-admin users may not see HELIX in Genie. Grant manually:")
        info(f"  databricks workspace update-permissions directories {obj_id} \\")
        info(
            '    --json \'{"access_control_list":[{"group_name":"users","permission_level":"CAN_READ"}]}\''
        )
        return False


def upload(bundle: Path, target: str, client=None) -> int:
    from databricks.sdk.service.workspace import ImportFormat

    if client is None:
        client = make_client()
    w = client
    me = w.current_user.me()
    info(f"authenticated as {me.user_name}")

    files = [p for p in bundle.rglob("*") if p.is_file()]
    if not files:
        fail(3, f"bundle has no files: {bundle}")
    parent_dirs = sorted(
        {str(Path(target) / p.relative_to(bundle).parent) for p in files}
    )

    info(f"target: {target}")
    info(f"{len(files)} files / {len(parent_dirs)} dirs")

    for d in parent_dirs:
        d_norm = d.rstrip("/.") or target
        try:
            w.workspace.mkdirs(path=d_norm)
        except Exception as exc:
            print(f"  mkdirs warn {d_norm}: {exc}", file=sys.stderr)

    uploaded = failed = 0
    for f in files:
        rel = f.relative_to(bundle)
        dest = f"{target}/{rel.as_posix()}"
        try:
            with open(f, "rb") as fp:
                w.workspace.upload(
                    path=dest, content=fp, format=ImportFormat.AUTO, overwrite=True
                )
            uploaded += 1
            if uploaded % 50 == 0 or uploaded == len(files):
                info(f"uploaded {uploaded}/{len(files)}")
        except Exception as exc:
            failed += 1
            print(f"  FAIL {dest}: {exc}", file=sys.stderr)
    info(f"upload: {uploaded} ok, {failed} failed")
    return 4 if failed else 0


# --- verify ---------------------------------------------------------------


def verify(target: str, client=None) -> int:
    from databricks.sdk.service.workspace import ExportFormat
    import yaml

    if client is None:
        client = make_client()
    w = client

    try:
        objs = list(w.workspace.list(target))
        names = {o.path.split("/")[-1] for o in objs}
    except Exception as exc:
        fail(5, f"target dir not reachable: {exc}")
    if "SKILL.md" not in names:
        fail(5, f"SKILL.md not at {target}")

    try:
        export = w.workspace.export(path=f"{target}/SKILL.md", format=ExportFormat.AUTO)
        import base64

        body = base64.b64decode(export.content).decode("utf-8")
    except Exception as exc:
        fail(5, f"export failed: {exc}")
    end = body.find("\n---\n", 4)
    if end == -1:
        fail(5, "SKILL.md frontmatter not terminated")
    fm = yaml.safe_load(body[4:end])
    if fm.get("name") != SKILL_NAME:
        fail(5, f"SKILL.md frontmatter name != {SKILL_NAME!r}: {fm.get('name')!r}")
    info("verify: SKILL.md frontmatter OK")

    activities_path = f"{target}/references/activities"
    try:
        act_objs = list(w.workspace.list(activities_path))
        act_names = {o.path.split("/")[-1] for o in act_objs}
    except Exception as exc:
        fail(5, f"references/activities not reachable: {exc}")
    missing = [a for a in EXPECTED_ACTIVITIES if a not in act_names]
    if missing:
        fail(5, f"missing activity dirs: {', '.join(missing)}")
    info(f"verify: all {len(EXPECTED_ACTIVITIES)} activity dirs present")
    return 0


# --- target resolution ----------------------------------------------------


def resolve_target(args: argparse.Namespace, client) -> str:
    if args.target:
        return args.target.rstrip("/")
    if args.shared:
        return WORKSPACE_TARGET
    me = client.current_user.me()
    return f"/Users/{me.user_name}/.assistant/skills/{SKILL_NAME}"


# --- main -----------------------------------------------------------------


def _run(args: argparse.Namespace, client=None) -> int:
    """Shared install body. Used by both the CLI entry point (`main`) and
    the programmatic entry point (`install`)."""
    print("HELIX Genie installer")
    print()

    if client is None:
        info("connecting to workspace")
        client = make_client()

    target = resolve_target(args, client)
    print(f"target:  {target}")
    print(
        f"source:  {'local bundle' if args.bundle else ('main branch' if args.main else (args.version or 'latest release'))}"
    )
    print(f"repo:    {args.repo}")
    print()

    with tempfile.TemporaryDirectory(prefix="helix-genie-install-") as td:
        workdir = Path(td)
        bundle = acquire_bundle(args, workdir)
        validate_bundle(bundle)

        rc = upload(bundle, target, client)
        if rc != 0:
            return rc

        # For workspace-wide installs, ensure non-admin users can read the
        # skill — workspace ACL inheritance defaults to admin-only on
        # tightly-scoped tenants. Grant on the skill dir AND its parent
        # (Genie lists the parent to discover skills).
        if args.shared:
            parent = str(Path(target).parent)
            info("ensuring skill is readable by all workspace users")
            grant_users_read(client, parent)
            grant_users_read(client, target)

        rc = verify(target, client)
        if rc != 0:
            return rc

        if args.keep_temp:
            permanent = Path("/tmp/helix-genie-install-debug")
            if permanent.exists():
                shutil.rmtree(permanent)
            shutil.copytree(workdir, permanent)
            info(f"kept temp at {permanent}")

    print()
    print(f"HELIX installed at {target}")
    print()
    print("Next: open Databricks Genie Code (Agent mode) in your workspace")
    print('and try: "Using HELIX, list the routing modes you can route to."')
    return 0


def install(
    *,
    shared: bool = False,
    main: bool = False,
    version: str | None = None,
    bundle: "Path | str | None" = None,
    target: str | None = None,
    repo: str = DEFAULT_REPO,
    keep_temp: bool = False,
    client=None,
) -> int:
    """Programmatic entry point. Same semantics as the CLI.

    Designed to be invoked from a Databricks notebook cell where the
    kernel has implicit workspace credentials:

        import urllib.request, runpy
        urllib.request.urlretrieve(
            "https://github.com/easel/helix/releases/latest/download/genie-install",
            "/tmp/genie_install.py",
        )
        g = runpy.run_path("/tmp/genie_install.py")
        g["install"]()                # user-scoped, latest release
        # g["install"](shared=True)   # workspace-wide
        # g["install"](main=True)     # track main branch
    """
    args = argparse.Namespace(
        shared=shared,
        main=main,
        version=version,
        bundle=Path(bundle) if bundle else None,
        target=target,
        repo=repo,
        keep_temp=keep_temp,
    )
    if sum([bool(args.main), bool(args.version), bool(args.bundle)]) > 1:
        fail(2, "--main, --version, --bundle are mutually exclusive")
    if args.shared and args.target:
        fail(2, "--shared and --target are mutually exclusive")
    return _run(args, client=client)


def main() -> int:
    return _run(parse_args())


if __name__ == "__main__":
    sys.exit(main())
