#!/bin/bash

# Test script to create and process a new video with all 9 processors

API_KEY="muser_643fae7c05d14cf6bb896940311abb25_1774629545_b9f1a88f"
API_URL="http://localhost:3002"

echo "=== Testing New Video Processing with All 9 Processors ==="
echo ""

# 1. Create a test video copy with unique name
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/test_$(date +%s).mp4"

echo "1. Creating test video copy..."
cp "$SOURCE_VIDEO" "$TEST_VIDEO"
echo "   Created: $TEST_VIDEO"
echo ""

# 2. Register the new video
echo "2. Registering new 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\"}")

echo "$REGISTER_RESPONSE" | jq .
UUID=$(echo "$REGISTER_RESPONSE" | jq -r '.uuid')
echo "Video UUID: $UUID"
echo ""

# 3. Check if job was created
echo "3. Checking if job was created..."
sleep 3
JOB_RESPONSE=$(curl -s -X GET "$API_URL/api/v1/jobs" -H "X-API-Key: $API_KEY")
echo "$JOB_RESPONSE" | jq .
echo ""

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

# 5. Monitor progress for 30 seconds
echo "5. Monitoring progress (checking every 5 seconds for 30 seconds)..."
for i in {1..6}; do
	echo "--- Check $i at $(date) ---"
	curl -s -X GET "$API_URL/api/v1/progress/$UUID" -H "X-API-Key: $API_KEY" | jq '.processors[] | select(.status != "pending")'
	sleep 5
done
echo ""

# 6. Final status check
echo "6. Final status check..."
curl -s -X GET "$API_URL/api/v1/progress/$UUID" -H "X-API-Key: $API_KEY" | jq .
echo ""

# 7. Cleanup
echo "7. Cleaning up test video..."
rm "$TEST_VIDEO"
echo "   Removed: $TEST_VIDEO"
echo ""
echo "=== Test Complete ==="
