#!/bin/bash
#
# VNA System Startup Script
#
# This script starts the VNA System API server with proper environment setup
#

set -e  # Exit on any error

# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$(dirname "$SCRIPT_DIR")")"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Logging functions
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if we're in the right directory
if [ ! -f "$PROJECT_ROOT/vna_system/main.py" ]; then
    log_error "VNA System main.py not found. Please run this script from the project directory."
    exit 1
fi

# Change to project root
cd "$PROJECT_ROOT"

log_info "Starting VNA System from: $PROJECT_ROOT"

# Check Python version
if ! command -v python3 &> /dev/null; then
    log_error "Python 3 is not installed or not in PATH"
    exit 1
fi

PYTHON_VERSION=$(python3 --version 2>&1 | cut -d' ' -f2)
log_info "Using Python version: $PYTHON_VERSION"

# Check if virtual environment exists
if [ -d "venv" ] || [ -d ".venv" ]; then
    if [ -d "venv" ]; then
        VENV_PATH="venv"
    else
        VENV_PATH=".venv"
    fi

    log_info "Found virtual environment at: $VENV_PATH"
    log_info "Activating virtual environment..."

    # Activate virtual environment
    source "$VENV_PATH/bin/activate"
    log_success "Virtual environment activated"
else
    log_warning "No virtual environment found. Using system Python."
    log_warning "Consider creating a virtual environment: python3 -m venv venv"
fi

# Start the server
log_info "Starting VNA System API server..."
log_info "Press Ctrl+C to stop the server"
echo

# Run the main application
exec python3 -m vna_system.main


