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

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

PASS_COUNT=0
FAIL_COUNT=0

pass() {
  PASS_COUNT=$((PASS_COUNT + 1))
  printf "[PASS] %s\n" "$1"
}

fail() {
  FAIL_COUNT=$((FAIL_COUNT + 1))
  printf "[FAIL] %s\n" "$1"
}

assert_grep() {
  local pattern="$1"
  local file="$2"
  local desc="$3"
  if grep -E -q "$pattern" "$file"; then
    pass "$desc"
  else
    fail "$desc (pattern not found in $file)"
  fi
}

assert_cmd() {
  local desc="$1"
  shift
  if "$@" >/dev/null 2>&1; then
    pass "$desc"
  else
    fail "$desc"
  fi
}

printf "== Didactyl Trigger Feature Test ==\n"
printf "Root: %s\n" "$ROOT_DIR"

printf "\n-- Build check --\n"
if make -j >/tmp/didactyl-test-build.log 2>&1; then
  pass "Project builds successfully"
else
  fail "Project build failed (see /tmp/didactyl-test-build.log)"
fi

assert_cmd "Built binary exists" test -x ./didactyl

printf "\n-- Source wiring checks --\n"
assert_grep "TRIGGER_TYPE_WEBHOOK" src/trigger_manager.h "Webhook trigger type defined"
assert_grep "TRIGGER_TYPE_CRON" src/trigger_manager.h "Cron trigger type defined"
assert_grep "TRIGGER_TYPE_CHAIN" src/trigger_manager.h "Chain trigger type defined"

assert_grep "trigger_manager_fire\(" src/trigger_manager.c "Generic trigger fire API implemented"
assert_grep "trigger_manager_fire_chains\(" src/trigger_manager.c "Chain fire API implemented"
assert_grep "cron_matches_now\(" src/trigger_manager.c "Cron matcher implemented"
assert_grep "trigger_type_to_string" src/trigger_manager.c "Trigger type serialization implemented"

assert_grep "\"/api/trigger/\*\"" src/http_api.c "Webhook API route registered"
assert_grep "handle_trigger_webhook\(" src/http_api.c "Webhook handler implemented"

assert_grep "skill_create trigger must be one of: nostr-subscription, webhook, cron, chain, dm" src/tools/tool_skill.c "skill_create validation accepts supported trigger types"
assert_grep "skill_edit trigger must be one of: nostr-subscription, webhook, cron, chain, dm" src/tools/tool_skill.c "skill_edit validation accepts supported trigger types"

assert_grep "trigger_manager_fire_chains\(" src/agent.c "Post-execution chain hook wired in agent"

printf "\n-- Docs checks --\n"
assert_grep "\`webhook\`" docs/SKILLS.md "SKILLS docs include webhook trigger"
assert_grep "\`cron\`" docs/SKILLS.md "SKILLS docs include cron trigger"
assert_grep "\`chain\`" docs/SKILLS.md "SKILLS docs include chain trigger"
assert_grep "POST /api/trigger/:d_tag" docs/API.md "API docs include webhook endpoint"

printf "\n-- Optional local API probe --\n"
if command -v curl >/dev/null 2>&1; then
  HTTP_CODE=""
  HTTP_CODE="$(curl -k -s --connect-timeout 2 --max-time 5 -o /tmp/didactyl-trigger-probe.out -w "%{http_code}" https://127.0.0.1:8484/api/trigger/nonexistent -X POST -H 'Content-Type: application/json' -d '{}' || true)"
  if [[ "$HTTP_CODE" == "000" ]]; then
    HTTP_CODE="$(curl -s --connect-timeout 2 --max-time 5 -o /tmp/didactyl-trigger-probe.out -w "%{http_code}" http://127.0.0.1:8484/api/trigger/nonexistent -X POST -H 'Content-Type: application/json' -d '{}' || true)"
  fi

  if [[ "$HTTP_CODE" == "200" || "$HTTP_CODE" == "400" || "$HTTP_CODE" == "404" || "$HTTP_CODE" == "503" ]]; then
    pass "Webhook endpoint responds on local API (http code: $HTTP_CODE)"
  elif [[ "$HTTP_CODE" == "000" || -z "$HTTP_CODE" ]]; then
    pass "Webhook endpoint probe skipped (no local API listener detected)"
  else
    fail "Webhook endpoint probe did not get expected response (http code: ${HTTP_CODE:-none})"
  fi
else
  fail "curl not available for API probe"
fi

printf "\n== Result ==\n"
printf "Pass: %d\n" "$PASS_COUNT"
printf "Fail: %d\n" "$FAIL_COUNT"

if [[ "$FAIL_COUNT" -gt 0 ]]; then
  exit 1
fi

exit 0
