#!/usr/bin/env bash
#
# test-library.sh - Validate the full PM skills library surface.
#
# Runs:
#   1) skill metadata checks
#   2) skill trigger-readiness audit
#   3) command metadata/reference checks
#   4) optional skill smoke tests
#   5) catalog generation freshness check
#

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

RUN_SMOKE=false

print_help() {
    cat <<EOF_HELP
Usage: $0 [--smoke]

Options:
  --smoke   Run additional skill smoke tests via scripts/test-a-skill.sh --smoke
  --help    Show this help
EOF_HELP
}

parse_args() {
    while [[ $# -gt 0 ]]; do
        case "$1" in
            --smoke)
                RUN_SMOKE=true
                shift
                ;;
            --help|-h)
                print_help
                exit 0
                ;;
            *)
                echo "Error: Unknown option '$1'" >&2
                print_help
                exit 1
                ;;
        esac
    done
}

main() {
    parse_args "$@"
    cd "$PROJECT_ROOT"

    echo "[1/5] Validating skills"
    python3 "$SCRIPT_DIR/check-skill-metadata.py"

    echo "[2/5] Auditing trigger metadata"
    python3 "$SCRIPT_DIR/check-skill-triggers.py"

    echo "[3/5] Validating commands"
    python3 "$SCRIPT_DIR/check-command-metadata.py"

    if $RUN_SMOKE; then
        echo "[4/5] Running skill smoke tests"
        "$SCRIPT_DIR/test-a-skill.sh" --smoke
    else
        echo "[4/5] Skipping smoke tests (use --smoke to enable)"
    fi

    echo "[5/5] Regenerating catalogs"
    python3 "$SCRIPT_DIR/generate-catalog.py"

    echo "Library checks complete."
}

main "$@"
