#!/usr/bin/env bash
# Ensure the native Lightdash skill (developing-in-lightdash) is installed globally.
# The gemma-lightdash plugin depends on it for generic Lightdash documentation.
# Runs async on SessionStart — fast exit when skill already exists.

set -euo pipefail

SKILL_DIR="$HOME/.claude/skills/developing-in-lightdash"

# Already installed — nothing to do
if [ -d "$SKILL_DIR" ]; then
  exit 0
fi

# Check if lightdash CLI is available
if ! command -v lightdash &>/dev/null; then
  printf '{"systemMessage": "The gemma-lightdash plugin requires the native Lightdash skill, but the `lightdash` CLI is not installed. Install it with `npm install -g @lightdash/cli`, then run `lightdash install-skills --global` to install the native skill."}\n'
  exit 0
fi

# Install the native skill globally
if lightdash install-skills --global 2>/dev/null; then
  printf '{"systemMessage": "The native Lightdash skill (developing-in-lightdash) was auto-installed globally. It provides comprehensive Lightdash documentation that the gemma-lightdash plugin builds upon."}\n'
else
  printf '{"systemMessage": "Failed to auto-install the native Lightdash skill. Run `lightdash install-skills --global` manually to install it."}\n'
fi
