#!/bin/sh
# CGI endpoint for checking PeerTube import status
# Returns JSON response

# Set headers
printf "Content-Type: application/json\r\n"
printf "Access-Control-Allow-Origin: *\r\n"
printf "\r\n"

# Get job_id from query string or POST body
JOB_ID=""

if [ -n "$QUERY_STRING" ]; then
    JOB_ID=$(echo "$QUERY_STRING" | sed -n 's/.*job_id=\([^&]*\).*/\1/p')
fi

if [ -z "$JOB_ID" ] && [ "$REQUEST_METHOD" = "POST" ]; then
    INPUT=$(cat)
    if command -v jq >/dev/null 2>&1; then
        JOB_ID=$(echo "$INPUT" | jq -r '.job_id // empty')
    else
        JOB_ID=$(echo "$INPUT" | jsonfilter -e '@.job_id' 2>/dev/null)
    fi
fi

if [ -z "$JOB_ID" ]; then
    echo '{"error": "job_id is required"}'
    exit 0
fi

# Sanitize job_id (only allow alphanumeric and underscore)
JOB_ID=$(echo "$JOB_ID" | tr -cd 'a-zA-Z0-9_')

STATUS_FILE="/tmp/peertube-import-${JOB_ID}.status"
RESULT_FILE="/tmp/peertube-import-${JOB_ID}.json"
LOG_FILE="/tmp/peertube-import-${JOB_ID}.log"
PROGRESS_FILE="/tmp/peertube-import-${JOB_ID}.progress"

# Check if job exists
if [ ! -f "$STATUS_FILE" ]; then
    echo '{"error": "Job not found", "job_id": "'"$JOB_ID"'"}'
    exit 0
fi

STATUS=$(cat "$STATUS_FILE" 2>/dev/null || echo "unknown")
PROGRESS=$(cat "$PROGRESS_FILE" 2>/dev/null || echo "0")

# If completed or failed, return full result
if [ -f "$RESULT_FILE" ]; then
    if [ "$STATUS" = "completed" ] || [ "$STATUS" = "failed" ]; then
        cat "$RESULT_FILE"
        exit 0
    fi
fi

# Otherwise return status with progress and logs
LOGS=""
if [ -f "$LOG_FILE" ]; then
    LOGS=$(tail -3 "$LOG_FILE" 2>/dev/null | tr '\n' ' ' | sed 's/"/\\"/g' | head -c 300)
fi

cat << EOF
{
    "status": "$STATUS",
    "job_id": "$JOB_ID",
    "progress": $PROGRESS,
    "logs": "$LOGS"
}
EOF
