#!/bin/bash

# Kill any process running on port 3123
echo "Killing any existing processes on port 3123..."
fuser -k 3123/tcp 2>/dev/null || true

# Create virtual environment if it doesn't exist
if [ ! -d "venv" ]; then
    echo "Creating virtual environment..."
    python3 -m venv venv
fi

# Activate virtual environment
echo "Activating virtual environment..."
source venv/bin/activate

# Verify virtual environment is active
echo "Verifying virtual environment..."
which python3
python3 --version

# Install dependencies
echo "Installing dependencies..."
pip install -r requirements.txt

# Check for help flag
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
    echo ""
    echo "Pitch Deck Analysis Application"
    echo "=============================="
    echo "Usage: ./start.sh <file_path>"
    echo "Example: ./start.sh presentation.pdf"
    echo ""
    echo "The application will automatically upload the generated report."
    echo ""
    exit 0
fi

# Verify file exists
if [ -z "$1" ]; then
    echo "Error: No file specified"
    echo "Usage: ./start.sh <file_path>"
    exit 1
fi

if [ ! -f "$1" ]; then
    echo "Error: File '$1' not found"
    exit 1
fi

# Start the application with immediate feedback
echo "Starting pitch deck parser..."
echo "Processing file: $1"
echo "Python path: $(which python3)"
echo "Working directory: $(pwd)"
echo "----------------------------------------"

python3 app.py "$1"
