#!/bin/bash

# Comprehensive test of video processing with all 9 processors

API_KEY="muser_643fae7c05d14cf6bb896940311abb25_1774629545_b9f1a88f"
API_URL="http://localhost:3002"
LOG_FILE="/Users/accusys/momentry/log/momentry_worker.log"

echo "=== Comprehensive Video Processing Test ==="
echo "Testing: Job status logic with all 9 processors"
echo "Date: $(date)"
echo ""

# 1. Create test video
SOURCE_VIDEO="/Users/accusys/momentry/var/sftpgo/data/demo/ExaSAN PCIe series - Director Ou Yu-Zhi Shares His Experience.mp4"
TEST_VIDEO="/Users/accusys/momentry/var/sftpgo/data/demo/comprehensive_test_$(date +%s).mp4"

echo "1. Creating test video..."
cp "$SOURCE_VIDEO" "$TEST_VIDEO"
echo "   Source: $(basename "$SOURCE_VIDEO")"
echo "   Test: $(basename "$TEST_VIDEO")"
echo "   Size: $(du -h "$TEST_VIDEO" | cut -f1)"
echo ""

# 2. Register video
echo "2. Registering video..."
REGISTER_RESPONSE=$(curl -s -X POST "$API_URL/api/v1/register" \
	-H "Content-Type: application/json" \
	-H "X-API-Key: $API_KEY" \
	-d "{\"path\": \"$TEST_VIDEO\"}")

UUID=$(echo "$REGISTER_RESPONSE" | jq -r '.uuid')
VIDEO_ID=$(echo "$REGISTER_RESPONSE" | jq -r '.video_id')
JOB_ID=$(echo "$REGISTER_RESPONSE" | jq -r '.job_id')

echo "   UUID: $UUID"
echo "   Video ID: $VIDEO_ID"
echo "   Job ID: $JOB_ID"
echo "   Response: $(echo "$REGISTER_RESPONSE" | jq -r '.file_name')"
echo ""

# 3. Wait for job to be created
echo "3. Waiting for job creation (5 seconds)..."
sleep 5

# 4. Check job in database
echo "4. Checking job in database..."
echo "SELECT uuid, status, processors, completed_processors, failed_processors FROM monitor_jobs WHERE uuid = '$UUID';" | psql postgres://accusys:accusys@localhost:5432/momentry
echo ""

# 5. Check processor count
echo "5. Verifying processor count..."
PROCESSOR_COUNT=$(echo "SELECT array_length(processors, 1) as count FROM monitor_jobs WHERE uuid = '$UUID';" | psql -t -q postgres://accusys:accusys@localhost:5432/momentry | tr -d ' ')
echo "   Processors configured: $PROCESSOR_COUNT"
if [ "$PROCESSOR_COUNT" -eq 9 ]; then
	echo "   ✅ SUCCESS: All 9 processors configured!"
else
	echo "   ❌ FAILED: Expected 9 processors, got $PROCESSOR_COUNT"
fi
echo ""

# 6. Monitor progress for 60 seconds
echo "6. Monitoring progress for 60 seconds (checking every 10 seconds)..."
echo "   Time  | Status    | Completed | Failed | Running/Pending"
echo "   ------|-----------|-----------|--------|----------------"

for i in {1..6}; do
	sleep 10
	CURRENT_TIME=$((i * 10))

	# Get job status from database
	JOB_STATUS=$(echo "SELECT status, 
        array_length(completed_processors, 1) as completed,
        array_length(failed_processors, 1) as failed,
        (array_length(processors, 1) - array_length(completed_processors, 1) - array_length(failed_processors, 1)) as remaining
        FROM monitor_jobs WHERE uuid = '$UUID';" | psql -t -q postgres://accusys:accusys@localhost:5432/momentry | tr -d ' ')

	STATUS=$(echo "$JOB_STATUS" | cut -d'|' -f1)
	COMPLETED=$(echo "$JOB_STATUS" | cut -d'|' -f2)
	FAILED=$(echo "$JOB_STATUS" | cut -d'|' -f3)
	REMAINING=$(echo "$JOB_STATUS" | cut -d'|' -f4)

	printf "   %4ds | %-9s | %9s | %6s | %15s\n" "$CURRENT_TIME" "$STATUS" "$COMPLETED" "$FAILED" "$REMAINING"
done
echo ""

# 7. Final status check
echo "7. Final status check..."
FINAL_STATUS=$(echo "SELECT 
    status,
    processors,
    completed_processors,
    failed_processors,
    array_length(processors, 1) as total,
    array_length(completed_processors, 1) as completed,
    array_length(failed_processors, 1) as failed
    FROM monitor_jobs WHERE uuid = '$UUID';" | psql -t -q -A -F '|' postgres://accusys:accusys@localhost:5432/momentry)

IFS='|' read -r STATUS PROCESSORS COMPLETED FAILED TOTAL COMPLETED_COUNT FAILED_COUNT <<<"$FINAL_STATUS"

echo "   Final Status: $STATUS"
echo "   Total Processors: $TOTAL"
echo "   Completed: $COMPLETED_COUNT"
echo "   Failed: $FAILED_COUNT"
echo "   Processors list: $PROCESSORS"
echo ""

# 8. Analysis
echo "8. Test Analysis:"
if [ "$TOTAL" -eq 9 ]; then
	echo "   ✅ Processor count correct: 9"
else
	echo "   ❌ Processor count incorrect: $TOTAL (expected 9)"
fi

if [ "$STATUS" = "completed" ] && [ "$COMPLETED_COUNT" -eq 9 ]; then
	echo "   ✅ Job correctly completed with all 9 processors"
elif [ "$STATUS" = "failed" ] && [ "$FAILED_COUNT" -gt 0 ]; then
	echo "   ⚠️  Job failed (some processors failed)"
	echo "   ✅ Job correctly marked as failed (not prematurely completed)"
elif [ "$STATUS" = "running" ]; then
	echo "   ⚠️  Job still running"
	echo "   ✅ Job correctly not marked as completed prematurely"
else
	echo "   ❌ Unexpected state: status=$STATUS, completed=$COMPLETED_COUNT, failed=$FAILED_COUNT"
fi
echo ""

# 9. Check worker logs for errors
echo "9. Checking worker logs for errors..."
LOG_ERRORS=$(tail -100 "$LOG_FILE" | grep -i "error\|failed" | head -5)
if [ -n "$LOG_ERRORS" ]; then
	echo "   Recent errors in logs:"
	echo "$LOG_ERRORS" | sed 's/^/     /'
else
	echo "   ✅ No recent errors in logs"
fi
echo ""

# 10. Cleanup
echo "10. Cleaning up..."
rm "$TEST_VIDEO"
echo "   Removed test video: $(basename "$TEST_VIDEO")"
echo ""

echo "=== Test Complete ==="
echo "Summary:"
echo "- Job created with UUID: $UUID"
echo "- Total processors configured: $TOTAL"
echo "- Final status: $STATUS"
echo "- Processors completed: $COMPLETED_COUNT"
echo "- Processors failed: $FAILED_COUNT"
echo ""
echo "The key verification is that the job waits for all 9 processors"
echo "before marking itself as 'completed', which was the original issue."
