#!/usr/bin/env bash
set -euo pipefail

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

# Legacy folder names are retained for install compatibility; frontmatter now
# exposes dcu-profiler-report and lightop-kernel-agent-loop.
skill_dir="$PROJECT_ROOT/skills/ncu-report"
skill_file="$skill_dir/SKILL.md"
metrics_file="$skill_dir/references/metrics.md"
examples_file="$skill_dir/references/examples.md"
script_file="$skill_dir/scripts/dcu_profile_digest.py"
install_script="$PROJECT_ROOT/scripts/install-skill.sh"
kernel_skill="$PROJECT_ROOT/skills/humanize-kernel-agent-loop/SKILL.md"

fail() {
    echo "FAIL: $*" >&2
    exit 1
}

[[ -f "$skill_file" ]] || fail "missing dcu-profiler-report skill"
[[ -f "$metrics_file" ]] || fail "missing dcu-profiler-report metrics reference"
[[ -f "$examples_file" ]] || fail "missing dcu-profiler-report examples reference"
[[ -f "$script_file" ]] || fail "missing DCU profile digest helper"

grep -q 'name: dcu-profiler-report' "$skill_file" \
    || fail "profiler skill frontmatter is not dcu-profiler-report"
grep -q 'name: lightop-kernel-agent-loop' "$kernel_skill" \
    || fail "kernel agent skill frontmatter is not lightop-kernel-agent-loop"
grep -q 'lightop-kernel-knowledge' "$install_script" \
    || fail "install script does not install lightop-kernel-knowledge"
grep -q 'dcu-profiler-report' "$skill_file" \
    || fail "skill does not use the DCU profiler name"
grep -q 'profile first, diagnose second, optimize third' "$skill_file" \
    || fail "skill does not encode profiling-grounded rule"
grep -q 'hipprof' "$skill_file" \
    || fail "skill does not require hipprof first-pass profiling"
grep -q 'rocprof-compute' "$skill_file" \
    || fail "skill does not cover deeper ROCm profiler escalation"
grep -q 'AMDGPU ISA' "$skill_file" \
    || fail "skill does not cover AMDGPU ISA/code-object inspection"
grep -q 'exactly one next edit' "$skill_file" \
    || fail "skill does not force a single concrete edit"
grep -q 'torch.cuda.synchronize' "$examples_file" \
    || fail "examples lack explicit synchronization pattern"
grep -q 'VGPR' "$metrics_file" \
    || fail "metrics reference lacks resource-pressure signals"
grep -q 'MFMA' "$metrics_file" \
    || fail "metrics reference lacks MFMA compute-path signals"
grep -q 'dcu-profiler-report' "$kernel_skill" \
    || fail "kernel agent loop does not call dcu-profiler-report"

python3 -m py_compile "$script_file"

echo "PASS: dcu-profiler-report skill is wired"
