#!/bin/bash

# Script to trigger harvest for testing
echo "🚀 Triggering Harvest Test"
echo "=========================="
echo ""

# Create test message with various patterns
MESSAGE='I prefer using Python for data science projects. My favorite framework is FastAPI. 
I decided to switch from MongoDB to PostgreSQL for better consistency. 
My goal is to become an expert in distributed systems. 
I am a software engineer working on AI systems. 
Actually, let me correct that - I specialize in machine learning engineering.
I feel excited about the progress we are making with this memory system!'

echo "Sending test message with multiple patterns..."
echo ""

# Try different methods to send to MCP

# Method 1: Direct stdio (if MCP is running interactively)
if pgrep -f "codex-memory mcp-stdio" > /dev/null; then
    echo "Found MCP stdio process, attempting to send..."
    
    # Create the JSON-RPC request
    REQUEST=$(cat <<EOF
{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "harvest_conversation",
    "arguments": {
      "message": "$MESSAGE",
      "force_harvest": true,
      "silent_mode": false
    }
  },
  "id": 1
}
EOF
)
    
    echo "$REQUEST" > /tmp/harvest_test.json
    echo "Request saved to /tmp/harvest_test.json"
    echo ""
    echo "Since MCP is running via stdio, you need to send this to the MCP process."
    echo "You can do this by:"
    echo ""
    echo "1. Finding the MCP process terminal/window"
    echo "2. Pasting this command there:"
    echo ""
    echo "$REQUEST"
    echo ""
fi

# Method 2: Check if there's a web API running
if curl -s http://localhost:3000/health > /dev/null 2>&1; then
    echo "Found web API on port 3000, sending harvest request..."
    curl -X POST http://localhost:3000/api/harvest \
      -H "Content-Type: application/json" \
      -d "{\"message\": \"$MESSAGE\", \"force_harvest\": true}" \
      2>/dev/null
    echo "✅ Harvest triggered via web API"
fi

# Method 3: Direct database insert (fallback for testing)
echo ""
echo "Alternative: Directly insert a test harvested memory..."
echo "Run this SQL command to simulate a harvested memory:"
echo ""
cat <<'SQL'
psql -U postgres -d codex_memory -c "
INSERT INTO memories (content, embedding, metadata, tier, importance_score)
VALUES (
    'TEST: I prefer Python for data science',
    ARRAY[0.1, 0.2, 0.3]::vector,
    '{\"pattern_type\": \"Preference\", \"extraction_confidence\": 0.85, \"harvester_version\": \"1.0\"}'::jsonb,
    'working',
    0.85
);"
SQL

echo ""
echo "Watch your monitor - it should update when memories are harvested!"