#!/bin/bash
# Main workflow: monitors prompt.txt and triggers AI responses

PROMPT_FILE="prompt.txt"
RESPONSE_FILE="respons.txt"
LAST_HASH=""

echo "=== Prompt Iteration Workflow Started ==="
echo "Monitoring: $PROMPT_FILE"
echo "Responses will be written to: $RESPONSE_FILE"
echo "------------------------------------------"

while true; do
    if [ -f "$PROMPT_FILE" ]; then
        CURRENT_HASH=$(md5sum "$PROMPT_FILE" 2>/dev/null | awk '{print $1}')
        
        # Detect change
        if [ "$CURRENT_HASH" != "$LAST_HASH" ] && [ -n "$LAST_HASH" ]; then
            echo ""
            echo ">>> Change detected in $PROMPT_FILE at $(date)"
            echo ">>> AI should process the new prompt and write response to $RESPONSE_FILE"
            LAST_HASH="$CURRENT_HASH"
            
            # Signal that a response is needed
            echo "PENDING" > .prompt_status
        elif [ -z "$LAST_HASH" ]; then
            LAST_HASH="$CURRENT_HASH"
            echo "Initial file hash captured"
        fi
    fi
    sleep 1
done
