#!/bin/bash

# Resolve script directory and change to ai-hub root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR/.."

# ==============================================================================
# Script to scan for specific file types and upload their content to an API.
# Prerequisites:
# - The FastAPI application must be running locally on http://localhost:8000.
# - The 'jq' command-line JSON processor must be installed.
# - The 'curl' command-line tool must be installed.
# ==============================================================================

# Define the API endpoint
API_URL="http://localhost:8000/documents"

DEFAULT_MODEL="gemini"
CURRENT_MODEL="" # The model used in the last turn

# --- 1. Check for Dependencies ---
if ! command -v jq &> /dev/null
then
    echo "❌ 'jq' is not installed. Please install it to run this script."
    exit 1
fi

# --- 2. Start the FastAPI Server in the Background ---
echo "--- Starting AI Hub Server ---"
uvicorn app.main:app --host 127.0.0.1 --port 8000 &
SERVER_PID=$!

# Define a cleanup function to kill the server on exit
cleanup() {
    echo ""
    echo "--- Shutting Down Server (PID: $SERVER_PID) ---"
    kill $SERVER_PID
}
# Register the cleanup function to run when the script exits (e.g., Ctrl+C or typing 'exit')
trap cleanup EXIT

echo "Server started with PID: $SERVER_PID. Waiting for it to initialize..."
sleep 5 # Wait for the server to be ready


# Find all files with the specified extensions in the current directory and its subdirectories
# The -print0 option is used to handle filenames with spaces or special characters.
find . -type f \( -name "*.py" -o -name "*.txt" -o -name "*.md" -o -name "*.yaml" \) -print0 | while IFS= read -r -d $'\0' file_path; do
    
    # Get the file's basename (e.g., "my_file.md") to use as the title
    file_title=$(basename -- "$file_path")
    
    # Get the file creation date.
    # Note: 'stat' options differ between Linux and macOS.
    # Linux: stat -c %w
    # macOS: stat -f %B
    # This script assumes a Linux-like stat output. You may need to adjust this.
    file_created_at=$(stat -c %w "$file_path")

    # Read the entire file content into a variable.
    # `cat` is used to get the content, and it's escaped for JSON.
    file_content=$(cat "$file_path" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/`/\\`/g')

    # Construct the JSON payload using `jq` for robust formatting
    json_payload=$(jq -n \
                    --arg title "$file_title" \
                    --arg text "$file_content" \
                    --arg file_path "$file_path" \
                    '{title: $title, text: $text, file_path: $file_path}')

    echo "Uploading file: $file_path"

    # Make the POST request to the API
    response=$(curl -X POST \
                    -s \
                    -H "Content-Type: application/json" \
                    -d "$json_payload" \
                    "$API_URL")

    # Display the response from the API
    if [[ "$response" == *"success"* ]]; then
        echo "✅ Uploaded successfully. Response: $response"
    else
        echo "❌ Failed to upload. Response: $response"
    fi

    echo "---"

done

echo "Script finished."
