#!/bin/bash
# tests/cli-mode/test-agent-tool-access.sh
# Phase 2 :: Validates agent tool access configuration in CLI mode (Priority 2)

set -euo pipefail

PROJECT_ROOT=$(git rev-parse --show-toplevel)
source "$PROJECT_ROOT/tests/test-utils.sh"

# Test counters
PASS_COUNT=0
TOTAL_COUNT=0

pass() { echo "✅ PASS: $1"; PASS_COUNT=$((PASS_COUNT + 1)); TOTAL_COUNT=$((TOTAL_COUNT + 1)); return 0; }
fail() { echo "❌ FAIL: $1"; TOTAL_COUNT=$((TOTAL_COUNT + 1)); return 0; }

cleanup() {
  log_info "Cleanup complete - smoke test only, no processes spawned"
}
trap cleanup EXIT

test_required_tools_list() {
  log_step "GIVEN required tools for CLI mode agents"

  # WHEN checking tool list definition
  local required_tools=(
    "Bash"
    "Read"
    "Write"
    "Edit"
    "Grep"
    "Glob"
    "Task"
  )

  # THEN verify tool list is documented
  log_info "Required tools for CLI mode agents (7 tools):"
  for tool in "${required_tools[@]}"; do
    log_info "  - $tool"
    pass "Tool $tool is in required list"
  done

  log_info "✅ Required tools list validation passed (7 tools)"
}

test_spawn_agent_tool_config() {
  log_step "GIVEN spawn-agent.sh tool configuration"

  # WHEN checking spawn-agent.sh for tool access setup
  local spawn_agent="$PROJECT_ROOT/.claude/skills/cfn-agent-spawning/spawn-agent.sh"

  # THEN verify spawn-agent.sh exists
  if [[ -f "$spawn_agent" ]]; then
    pass "spawn-agent.sh exists"
  else
    fail "spawn-agent.sh exists"
    return
  fi

  # Verify tool configuration is set up with actual tool access validation
  if grep -q "tool\|TOOL" "$spawn_agent" 2>/dev/null; then
    # Check if specific tool access patterns are present
    if grep -q "npm\|claude\|agent" "$spawn_agent" 2>/dev/null; then
      pass "spawn-agent.sh contains agent execution configuration"
    else
      fail "spawn-agent.sh contains tool references but lacks agent execution patterns"
    fi
  else
    fail "spawn-agent.sh missing tool configuration for agent spawning"
  fi

  log_info "✅ spawn-agent.sh tool configuration validation passed"
}

test_agent_prompt_builder_tool_access() {
  log_step "GIVEN agent prompt builder tool injection"

  # WHEN checking agent-prompt-builder.ts for tool access
  local prompt_builder="$PROJECT_ROOT/src/cli/agent-prompt-builder.ts"

  # THEN verify prompt builder exists
  if [[ -f "$prompt_builder" ]]; then
    pass "agent-prompt-builder.ts exists"
  else
    fail "agent-prompt-builder.ts exists"
    return
  fi

  # Verify tool access injection logic
  if grep -q "tool" "$prompt_builder" 2>/dev/null; then
    pass "agent-prompt-builder.ts handles tool access"
  else
    fail "agent-prompt-builder.ts handles tool access"
  fi

  log_info "✅ agent-prompt-builder.ts tool access validation passed"
}

test_bash_tool_access() {
  log_step "GIVEN Bash tool access requirement"

  # WHEN checking Bash tool availability
  local tool_name="Bash"

  # THEN verify agents can execute bash commands
  log_info "Validating Bash tool access for coordination and file operations"

  # Check if spawn-agent.sh enables bash execution through shell access
  local spawn_agent_bash_access=false
  if grep -q "bash\|Bash\|sh\|exec\|spawn" "$PROJECT_ROOT/.claude/skills/cfn-agent-spawning/spawn-agent.sh" 2>/dev/null; then
    spawn_agent_bash_access=true
  fi
  
  # Verify coordination scripts actually use bash commands
  local coordination_bash_usage=false
  if find "$PROJECT_ROOT/.claude/skills/cfn-loop-orchestration/" -name "*.sh" -type f -exec grep -l "redis-cli\|bash\|exec" {} \; 2>/dev/null | head -1 | grep -q .; then
    coordination_bash_usage=true
  fi
  
  if [ "$spawn_agent_bash_access" = true ] && [ "$coordination_bash_usage" = true ]; then
    pass "Bash tool access properly configured for coordination"
  else
    fail "Bash tool access insufficient - spawn: $spawn_agent_bash_access, coordination: $coordination_bash_usage"
  fi

  # Verify coordination uses bash commands (Redis CLI)
  if grep -rq "redis-cli" "$PROJECT_ROOT/.claude/skills/cfn-loop-orchestration/" 2>/dev/null; then
    pass "Coordination requires Bash tool for Redis CLI commands"
  else
    fail "Coordination requires Bash tool for Redis CLI commands"
  fi

  log_info "✅ Bash tool access validation passed"
}

test_file_operation_tools() {
  log_step "GIVEN file operation tools (Read, Write, Edit)"

  # WHEN checking file operation tool requirements
  local file_tools=("Read" "Write" "Edit")

  # THEN verify file tools are essential for agent work
  log_info "Validating file operation tools for agent work"

  # Validate file operation tools are actually available to agents
  local tools_documented=0
  local tools_available=0
  
  for tool in "${file_tools[@]}"; do
    # Check if tools are documented in project configuration
    if grep -qi "$tool" "$PROJECT_ROOT/CLAUDE.md" 2>/dev/null || \
       grep -qi "$tool" "$PROJECT_ROOT/.claude/agents/cfn-dev-team/README.md" 2>/dev/null || \
       grep -qi "$tool" "$PROJECT_ROOT/src/cli/agent-definition-parser.ts" 2>/dev/null; then
      ((tools_documented++))
    fi
    
    # Check if tools are implemented in the codebase
    if find "$PROJECT_ROOT/src" -name "*.ts" -type f -exec grep -l "$tool" {} \; 2>/dev/null | head -1 | grep -q .; then
      ((tools_available++))
    fi
  done
  
  if [ "$tools_documented" -eq "${#file_tools[@]}" ] && [ "$tools_available" -eq "${#file_tools[@]}" ]; then
    pass "All file operation tools documented and implemented ($tools_documented/${#file_tools[@]})"
  else
    fail "File operation tools incomplete - documented: $tools_documented/${#file_tools[@]}, available: $tools_available/${#file_tools[@]}"
  fi

  log_info "✅ File operation tools validation passed"
}

test_search_tools() {
  log_step "GIVEN search tools (Grep, Glob)"

  # WHEN checking search tool requirements
  local search_tools=("Grep" "Glob")

  # THEN verify search tools are available
  log_info "Validating search tools for code navigation"

  # Check if CLAUDE.md mentions these tools (case-insensitive)
  if grep -qi "grep" "$PROJECT_ROOT/CLAUDE.md" 2>/dev/null; then
    pass "Search tools documented in CLAUDE.md"
  else
    fail "Search tools documented in CLAUDE.md"
  fi

  # Verify preferred usage patterns
  if grep -iq "grep.*over.*find\|prefer.*grep" "$PROJECT_ROOT/CLAUDE.md" 2>/dev/null; then
    pass "Grep preferred over find (performance requirement)"
  else
    fail "Grep preferred over find (performance requirement)"
  fi

  log_info "✅ Search tools validation passed"
}

test_task_tool_access() {
  log_step "GIVEN Task tool for agent spawning"

  # WHEN checking Task tool availability
  local tool_name="Task"

  # THEN verify Task tool is documented for agent spawning
  log_info "Validating Task tool for sub-agent spawning"

  # Check if Task spawning is mentioned in coordination
  if grep -q "Task(" "$PROJECT_ROOT/CLAUDE.md" 2>/dev/null; then
    pass "Task tool documented for agent spawning"
  else
    fail "Task tool documented for agent spawning"
  fi

  # Verify CLI mode uses proper spawning mechanism (not Task tool directly)
  local cli_spawning_mechanism=false
  local spawn_script_exists=false
  
  # Check for CLI spawning documentation
  if grep -q "spawn-agent\|CLI.*agent\|npx.*claude" "$PROJECT_ROOT/.claude/commands/cfn-loop-cli.md" 2>/dev/null; then
    cli_spawning_mechanism=true
  fi
  
  # Check for spawn script existence and functionality
  if [[ -f "$PROJECT_ROOT/.claude/skills/cfn-agent-spawning/spawn-agent.sh" ]] && \
     grep -q "agent\|spawn\|execute" "$PROJECT_ROOT/.claude/skills/cfn-agent-spawning/spawn-agent.sh" 2>/dev/null; then
    spawn_script_exists=true
  fi
  
  if [ "$cli_spawning_mechanism" = true ] && [ "$spawn_script_exists" = true ]; then
    pass "CLI mode spawning properly configured"
  else
    fail "CLI mode spawning defective - documentation: $cli_spawning_mechanism, script: $spawn_script_exists"
  fi

  log_info "✅ Task tool access validation passed"
}

test_tool_permissions_in_spawn() {
  log_step "GIVEN tool permission configuration in spawn-agent.sh"

  # WHEN checking tool permissions setup
  local spawn_agent="$PROJECT_ROOT/.claude/skills/cfn-agent-spawning/spawn-agent.sh"

  # THEN verify no tool restrictions are in place
  log_info "Validating tool permissions are not restricted"

  # Check if there are any tool restrictions
  if grep -q "disable.*tool\|restrict.*tool\|tool.*permission" "$spawn_agent" 2>/dev/null; then
    fail "Tool permissions should not be restricted in spawn-agent.sh"
  else
    pass "No tool restrictions found in spawn-agent.sh"
  fi

  # Verify agents have full tool access by checking agent definitions
  local required_tools_count=7
  local tools_found=0
  
  # Check agent definition files for tool configuration
  if find "$PROJECT_ROOT/.claude/agents" -name "*.md" -type f -exec grep -l "tools:" {} \; 2>/dev/null | head -1 | grep -q .; then
    # Count actual tools mentioned in agent definitions
    local tool_count=$(grep -h "tools:" "$PROJECT_ROOT/.claude/agents"/*.md 2>/dev/null | grep -o "\[.*\]" | grep -o "," | wc -l || echo 0)
    tools_found=$((tool_count + 1))  # +1 because grep -o "," gives count of commas, not tools
  fi
  
  # Also check in agent-executor.ts for tool handling
  if grep -q "tools.*length\|convertToolNames" "$PROJECT_ROOT/src/cli/agent-executor.ts" 2>/dev/null; then
    tools_found=$((tools_found + 1))
  fi
  
  if [ "$tools_found" -ge "$required_tools_count" ]; then
    pass "Tool access verified ($tools_found tools available)"
  else
    fail "Insufficient tool access - found $tools_found, expected $required_tools_count"
  fi

  log_info "✅ Tool permissions validation passed"
}

test_coordination_tool_requirements() {
  log_step "GIVEN coordination protocol tool requirements"

  # WHEN checking coordination protocol dependencies
  local coordination_dir="$PROJECT_ROOT/.claude/skills/cfn-redis-coordination"

  # THEN verify coordination skill directory exists
  if [[ -d "$coordination_dir" ]]; then
    pass "Coordination skill directory exists"

    # Check for Redis coordination scripts that actually use bash commands
    local bash_coordination_found=false
    local redis_coordination_found=false
    
    # Look for bash scripts in coordination
    if find "$coordination_dir" -name "*.sh" -type f -exec grep -l "redis-cli\|redis\|bash" {} \; 2>/dev/null | head -1 | grep -q .; then
      bash_coordination_found=true
    fi
    
    # Look for Redis usage patterns
    if find "$coordination_dir" -name "*.sh" -type f -exec grep -l "redis-cli\|lPush\|BLPOP" {} \; 2>/dev/null | head -1 | grep -q .; then
      redis_coordination_found=true
    fi
    
    if [ "$bash_coordination_found" = true ] && [ "$redis_coordination_found" = true ]; then
      pass "Coordination protocol properly configured with Redis and Bash"
    else
      fail "Coordination protocol incomplete - bash: $bash_coordination_found, redis: $redis_coordination_found"
    fi
  else
    fail "Coordination skill directory exists"
  fi

  log_info "✅ Coordination tool requirements validation passed"
}

test_pre_edit_backup_tool_requirements() {
  log_step "GIVEN pre-edit backup hook tool requirements"

  # WHEN checking pre-edit backup requirements
  local pre_edit_hook="$PROJECT_ROOT/.claude/hooks/cfn-invoke-pre-edit.sh"

  # THEN verify backup hook exists
  if [[ -f "$pre_edit_hook" ]]; then
    pass "Pre-edit backup hook exists"
  else
    fail "Pre-edit backup hook exists"
    return
  fi

  # Verify CLAUDE.md documents pre-edit backup requirement
  if grep -q "Pre-Edit Backup" "$PROJECT_ROOT/CLAUDE.md" 2>/dev/null; then
    pass "Pre-edit backup documented in CLAUDE.md"
  else
    fail "Pre-edit backup documented in CLAUDE.md"
  fi

  # Verify agents need Bash tool to invoke backup hook
  if grep -q "cfn-invoke-pre-edit.sh" "$PROJECT_ROOT/CLAUDE.md" 2>/dev/null; then
    pass "Pre-edit hook requires Bash tool for invocation"
  else
    fail "Pre-edit hook requires Bash tool for invocation"
  fi

  log_info "✅ Pre-edit backup tool requirements validation passed"
}

# Execute tests
test_required_tools_list
test_spawn_agent_tool_config
test_agent_prompt_builder_tool_access
test_bash_tool_access
test_file_operation_tools
test_search_tools
test_task_tool_access
test_tool_permissions_in_spawn
test_coordination_tool_requirements
test_pre_edit_backup_tool_requirements

# Test summary
echo ""
log_step "Test Summary"
PASS_RATE=$(awk "BEGIN {printf \"%.0f\", ($PASS_COUNT / $TOTAL_COUNT * 100)}")
echo -e "${GREEN}Total Tests: $TOTAL_COUNT${NC}"
echo -e "${GREEN}Passed: $PASS_COUNT${NC}"
if [[ $TOTAL_COUNT -ne $PASS_COUNT ]]; then
  echo -e "${RED}Failed: $((TOTAL_COUNT - PASS_COUNT))${NC}"
fi
echo -e "${GREEN}Pass Rate: ${PASS_RATE}%${NC}"

if [[ $PASS_COUNT -eq $TOTAL_COUNT ]]; then
  echo ""
  echo -e "${GREEN}✅ All agent tool access tests PASSED${NC}"
  echo ""
  log_info "Validation complete: Agent tool access configuration is correct"
  log_info "All 7 required tools validated: Bash, Read, Write, Edit, Grep, Glob, Task"
  log_info "Tool permissions properly configured in spawn-agent.sh"
  log_info "Coordination and pre-edit backup hooks require Bash tool"
  exit 0
else
  echo ""
  echo -e "${RED}❌ Some tests failed${NC}"
  exit 1
fi
