#!/usr/bin/env bash

# Function to escape JSON special characters
escape_json() {
    local input="$1"
    # Escape backslash, quotes, newlines, tabs, etc.
    echo "$input" | sed 's/\\/\\\\/g; s/"/\\"/g; s/	/\\t/g' | tr '\n' '\\' | sed 's/\\$/\\n/g'
}

# Function to collect multiple inputs
collect_inputs() {
    local inputs=()
    local counter=1
    
    echo "Enter your inputs (press CTRL+D or type 'END' to finish):"
    echo "---"
    
    while true; do
        echo -n "Input $counter: "
        read -r input
        
        # Check if user wants to terminate
        if [[ $? -ne 0 ]] || [[ "$input" == "END" ]]; then
            break
        fi
        
        # Add input to array
        inputs+=("$input")
        ((counter++))
    done
    
    # Generate JSON
    echo
    echo "JSON Output:"
    echo "{"
    echo '  "timestamp": "'$(date -Iseconds)'",'
    echo '  "total_inputs": '${#inputs[@]}','
    echo '  "inputs": ['
    
    for i in "${!inputs[@]}"; do
        local escaped=$(escape_json "${inputs[$i]}")
        if [[ $i -eq $((${#inputs[@]} - 1)) ]]; then
            echo "    {\"id\": $((i+1)), \"value\": \"$escaped\"}"
        else
            echo "    {\"id\": $((i+1)), \"value\": \"$escaped\"},"
        fi
    done
    
    echo "  ]"
    echo "}"
}

# Function for single input with parameters
single_input() {
    if [[ $# -eq 0 ]]; then
        echo -n "Enter your input: "
        read -r input
    else
        input="$*"
    fi
    
    local escaped=$(escape_json "$input")
    
    echo "{"
    echo '  "timestamp": "'$(date -Iseconds)'",'
    echo '  "input": "'$escaped'",'
    echo '  "length": '${#input}
    echo "}"
}

# Help function
show_help() {
    echo "Usage: $0 [OPTIONS] [TEXT]"
    echo
    echo "Converts user input to JSON format"
    echo
    echo "OPTIONS:"
    echo "  -m, --multiple    Multiple input mode"
    echo "  -s, --single      Single input mode (default)"
    echo "  -h, --help        Show this help"
    echo
    echo "EXAMPLES:"
    echo "  $0                          # Interactive single input"
    echo "  $0 \"Hello world\"            # Single input from parameter"
    echo "  $0 -m                       # Interactive multiple inputs"
    echo "  echo \"test\" | $0           # Input from pipe"
}

# Parameter handling
case "$1" in
    -h|--help)
        show_help
        exit 0
        ;;
    -m|--multiple)
        collect_inputs
        ;;
    -s|--single)
        shift
        single_input "$@"
        ;;
    *)
        # If there's no keyboard input and no parameters, read from pipe
        if [[ -t 0 ]] && [[ $# -eq 0 ]]; then
            single_input
        elif [[ ! -t 0 ]]; then
            # Read from pipe
            input=$(cat)
            escaped=$(escape_json "$input")
            echo "{"
            echo '  "timestamp": "'$(date -Iseconds)'",'
            echo '  "source": "pipe",'
            echo '  "input": "'$escaped'",'
            echo '  "length": '${#input}
            echo "}"
        else
            single_input "$@"
        fi
        ;;
esac