# shellcheck shell=bash

# Verify that the npm build output under `runtime/` is fresh and matches the
# source files that `okstra install` (src/install.mjs) copies into the user's
# `~/.claude/skills/`, `~/.claude/agents/`, and `~/.okstra/` directories.
#
# Historically this validator checked a *project-local* `.claude/skills/` +
# `.claude/agents/` tree that `okstra.sh` seeded into the project root on
# every run. That seeding step was removed when install moved into
# `src/install.mjs`; install now writes only to the user's
# `$HOME/.claude` + `$HOME/.okstra`, never to the project root. The runtime/
# tree is the canonical staging area that `okstra install` rsyncs from, so we
# validate parity there instead.
validate_seeded_assets() {
  local validation_mode="$1"
  local runtime_root="$WORKSPACE_ROOT/runtime"

  if [[ ! -d "$runtime_root" ]]; then
    printf 'runtime/ build output is missing — run `npm run build` before validating.\n' >&2
    return 1
  fi

  python3 - "$SOURCE_ASSET_ROOT" "$WORKSPACE_ROOT/skills" "$runtime_root" "$validation_mode" <<'PY'
from pathlib import Path
import sys

agents_source_root = Path(sys.argv[1])      # <repo>/agents
skills_source_root = Path(sys.argv[2])      # <repo>/skills
runtime_root = Path(sys.argv[3])            # <repo>/runtime
validation_mode = sys.argv[4]
errors = []


def check(source_path: Path, target_path: Path) -> None:
    if not target_path.is_file():
        errors.append(f"missing build-output asset: {target_path}")
        return
    if validation_mode == "match" and target_path.read_bytes() != source_path.read_bytes():
        errors.append(f"build-output asset does not match source: {target_path}")


# 1. Worker agent files: agents/workers/*-worker.md -> runtime/agents/workers/*-worker.md
#
workers_source = agents_source_root / "workers"
workers_target = runtime_root / "agents" / "workers"
if not workers_source.is_dir():
    errors.append(f"missing agents/workers source directory: {workers_source}")
else:
    for source_path in sorted(workers_source.glob("*.md")):
        check(source_path, workers_target / source_path.name)

# 2. Lead contract + internal lead resources: prompts/lead/* + prompts/coding-preflight/*
#    -> runtime/prompts/*. These ship as runtime resources (okstra install copies
#    runtime/prompts -> ~/.okstra/prompts), no longer as agent skills.
prompts_source_root = agents_source_root.parent / "prompts"
for sub in ("lead", "coding-preflight"):
    sub_root = prompts_source_root / sub
    if not sub_root.is_dir():
        errors.append(f"missing prompts source directory: {sub_root}")
        continue
    for source_path in sorted(sub_root.rglob("*.md")):
        relative = source_path.relative_to(prompts_source_root)
        check(source_path, runtime_root / "prompts" / relative)

# 3. Skill packages: skills/<name>/SKILL.md -> runtime/skills/<name>/SKILL.md
if not skills_source_root.is_dir():
    errors.append(f"missing skills source directory: {skills_source_root}")
else:
    for skill_dir in sorted(p for p in skills_source_root.iterdir() if p.is_dir()):
        for source_path in sorted(skill_dir.rglob("*.md")):
            relative = source_path.relative_to(skills_source_root)
            check(source_path, runtime_root / "skills" / relative)

if errors:
    for error in errors:
        print(error, file=sys.stderr)
    sys.exit(1)
PY
}
