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

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
tmp_root="$(mktemp -d)"
trap 'rm -rf "$tmp_root"' EXIT

mkdir -p "$tmp_root/ai/commands" "$tmp_root/ai/chunks/active"
cp "$script_dir/workflow-state.sh" "$tmp_root/ai/commands/workflow-state.sh"
cp "$script_dir/prompt-synthesize.sh" "$tmp_root/ai/commands/prompt-synthesize.sh"
chmod +x "$tmp_root/ai/commands/workflow-state.sh"
chmod +x "$tmp_root/ai/commands/prompt-synthesize.sh"
git -C "$tmp_root" init -q

assert_contains() {
  local output="$1"
  local expected="$2"
  if [[ "$output" != *"$expected"* ]]; then
    echo "Expected output to contain: $expected" >&2
    echo "Actual output:" >&2
    printf '%s\n' "$output" >&2
    exit 1
  fi
}

write_chunk() {
  local acceptance_status="$1"
  local qa_status="${2:-none}"
  local chunk="$tmp_root/ai/chunks/active/chunk-000999-test.md"

  cat > "$chunk" <<EOF
---
Status: Active
Owner Role: Developer
Created: 2026-05-10
Completed:
Depends On:
Validation: bash -n ai/commands/*.sh
---

# Test Chunk

## Execution Notes

- Implemented test fixture.
EOF

  if [[ "$acceptance_status" != "missing" ]]; then
    cat >> "$chunk" <<EOF

## Acceptance Criteria Verification

- Test criterion: $acceptance_status.
EOF
  fi

  cat >> "$chunk" <<'EOF'

## Pass History

### Developer Pass 1

- Role: Developer
- Date: 2026-05-10
- Goal: Test fixture.
- Result: Test fixture.
- Blockers: None.
- Validation: bash -n ai/commands/*.sh passed.
- Cleanup: No runtime artifacts created.
- Recommended Next Action: Hand off for QA review.
EOF

  if [[ "$qa_status" == "pass" ]]; then
    cat >> "$chunk" <<'EOF'

### QA Pass 1

- Role: QA
- Date: 2026-05-10
- Goal: Test fixture QA.
- Verdict: PASS
- Blockers: None.
- Acceptance Criteria: Verified.
- Validation: bash -n ai/commands/*.sh passed.
- Cleanup: No runtime artifacts created.
- Recommended Next Action: Complete/archive the chunk, then commit.

## QA Review

- Verdict: PASS
- Blockers: None.
- Acceptance Criteria: Verified.
- Runtime Smoke: Not applicable.
- Validation: bash -n ai/commands/*.sh passed.
- Cleanup: No runtime artifacts created.
- Recommended Next Action: Complete/archive the chunk, then commit.
EOF
  fi
}

write_prompt_history_chunk() {
  local with_qa_between="$1"
  local chunk="$tmp_root/ai/chunks/active/chunk-000999-test.md"

  cat > "$chunk" <<'EOF'
---
Status: Active
Owner Role: Developer
Created: 2026-05-10
Completed:
Depends On:
Validation: bash -n ai/commands/*.sh
---

# Test Chunk

## Execution Notes

- Implemented prompt history fixture.

## Acceptance Criteria Verification

- Test criterion: Verified.

## Pass History

### Developer Pass 1

- Role: Developer
- Date: 2026-05-10
- Goal: First Developer pass.
- Result: First pass result.
- Blockers: None.
- Validation: bash -n ai/commands/*.sh passed.
- Cleanup: No runtime artifacts created.
- Recommended Next Action: Hand off for QA review.
EOF

  if [[ "$with_qa_between" == "yes" ]]; then
    cat >> "$chunk" <<'EOF'

### QA Pass 1

- Role: QA
- Date: 2026-05-10
- Goal: Review first Developer pass.
- Verdict: PASS
- Blockers: None.
- Acceptance Criteria: Verified.
- Validation: bash -n ai/commands/*.sh passed.
- Cleanup: No runtime artifacts created.
- Recommended Next Action: Complete/archive the chunk, then commit.
EOF
  fi

  cat >> "$chunk" <<'EOF'

### Developer Pass 2

- Role: Developer
- Date: 2026-05-10
- Goal: Second Developer pass.
- Result: Second pass result.
- Blockers: None.
- Validation: bash -n ai/commands/*.sh passed.
- Cleanup: No runtime artifacts created.
- Recommended Next Action: Hand off for QA review.
EOF

  if [[ "$with_qa_between" == "yes" ]]; then
    cat >> "$chunk" <<'EOF'

## QA Review

- Verdict: STALE - previous PASS before Developer Pass 2.
- Blockers: Needs QA review for Developer Pass 2.
- Acceptance Criteria: Previously verified; Developer Pass 2 needs review.
- Runtime Smoke: Not applicable.
- Validation: Previous QA validation passed before Developer Pass 2.
- Cleanup: No runtime artifacts created.
- Recommended Next Action: Run QA review.
EOF
  fi
}

run_state() {
  "$tmp_root/ai/commands/workflow-state.sh" "$@"
}

write_chunk "Verified"
output="$(run_state)"
assert_contains "$output" "Canonical state: ready_for_qa"
assert_contains "$output" "Stale QA risk: no - QA pending for latest Developer pass"
assert_contains "$output" "Blockers: None for QA readiness"
run_state --ready-for-qa >/dev/null

if run_state --ready-to-complete >/tmp/workflow-state-ready-to-complete.out 2>&1; then
  echo "Expected ready-to-complete to fail before QA Review" >&2
  exit 1
fi
assert_contains "$(cat /tmp/workflow-state-ready-to-complete.out)" "current QA Review is missing"

write_chunk "missing"
if run_state --ready-for-qa >/tmp/workflow-state-ready-for-qa.out 2>&1; then
  echo "Expected ready-for-qa to fail without Acceptance Criteria Verification" >&2
  exit 1
fi
assert_contains "$(cat /tmp/workflow-state-ready-for-qa.out)" "Acceptance Criteria Verification is missing"

write_chunk "Blocked" "pass"
if run_state --ready-to-complete >/tmp/workflow-state-blocked-acceptance.out 2>&1; then
  echo "Expected ready-to-complete to fail with Blocked acceptance verification" >&2
  exit 1
fi
assert_contains "$(cat /tmp/workflow-state-blocked-acceptance.out)" "Acceptance Criteria Verification contains 1 Blocked item(s)"

write_prompt_history_chunk "no"
prompt_output="$("$tmp_root/ai/commands/prompt-synthesize.sh" qa)"
assert_contains "$prompt_output" "Relevant Pass History For QA:"
assert_contains "$prompt_output" "### Developer Pass 1"
assert_contains "$prompt_output" "### Developer Pass 2"

write_prompt_history_chunk "yes"
prompt_output="$("$tmp_root/ai/commands/prompt-synthesize.sh" qa)"
assert_contains "$prompt_output" "Relevant Pass History For QA:"
assert_contains "$prompt_output" "### Developer Pass 2"
if [[ "$prompt_output" == *"### Developer Pass 1"* ]]; then
  echo "Expected QA prompt to omit Developer passes already covered by latest QA pass" >&2
  exit 1
fi

echo "workflow-state scenario tests passed"
