#!/bin/bash

# Quick Start Guide for TaxCore Node.js Service
# This script helps you get started with the new TaxCore integration

echo "========================================"
echo "TaxCore Node.js Service - Quick Start"
echo "========================================"
echo ""

# Check Node.js
echo "1. Checking Node.js installation..."
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed!"
    echo "   Please install Node.js 14+ from https://nodejs.org/"
    exit 1
else
    NODE_VERSION=$(node --version)
    echo "✅ Node.js $NODE_VERSION is installed"
fi
echo ""

# Check npm
echo "2. Checking npm installation..."
if ! command -v npm &> /dev/null; then
    echo "❌ npm is not installed!"
    exit 1
else
    NPM_VERSION=$(npm --version)
    echo "✅ npm $NPM_VERSION is installed"
fi
echo ""

# Navigate to websocket-server directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR/websocket-server" || exit 1

# Install dependencies
echo "3. Installing dependencies..."
if [ ! -d "node_modules" ]; then
    npm install
    echo "✅ Dependencies installed"
else
    echo "✅ Dependencies already installed"
fi
echo ""

# Check for certificate
echo "4. Checking for TaxCore certificate..."
CERT_PATHS=(
    "../FRCS_Certs_Install/*.pfx"
    "/var/www/html/FRCS_Certs_Install/*.pfx"
    "/var/www/html/TAF/FRCS_Certs_Install/*.pfx"
)

CERT_FOUND=false
for path in "${CERT_PATHS[@]}"; do
    if ls $path 1> /dev/null 2>&1; then
        CERT_FILE=$(ls $path | head -n 1)
        echo "✅ Certificate found: $CERT_FILE"
        export TAXCORE_PFX_PATH="$CERT_FILE"
        CERT_FOUND=true
        break
    fi
done

if [ "$CERT_FOUND" = false ]; then
    echo "⚠️  Certificate not found in common locations"
    echo "   Service will attempt to autodiscover, but may fail"
    echo "   Set TAXCORE_PFX_PATH environment variable if needed"
fi
echo ""

# Check if .env exists
echo "5. Checking environment configuration..."
if [ -f "../.env" ]; then
    echo "✅ .env file found, loading configuration"
    export $(cat ../.env | grep -v '^#' | xargs)
else
    echo "⚠️  No .env file found, using default configuration"
    echo "   You can create a .env file in the project root with:"
    echo "   TAXCORE_SERVICE_PORT=3001"
    echo "   TAXCORE_PFX_PATH=/path/to/certificate.pfx"
    echo "   TAXCORE_PFX_PASSWORD=password"
fi
echo ""

# Check if port 3001 is available
echo "6. Checking port availability..."
PORT=${TAXCORE_SERVICE_PORT:-3001}
if lsof -Pi :$PORT -sTCP:LISTEN -t >/dev/null 2>&1; then
    echo "⚠️  Port $PORT is already in use"
    read -p "   Do you want to kill the existing process? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        lsof -ti:$PORT | xargs kill -9 2>/dev/null
        echo "✅ Killed existing process on port $PORT"
    else
        echo "   Please choose a different port using TAXCORE_SERVICE_PORT"
        exit 1
    fi
else
    echo "✅ Port $PORT is available"
fi
echo ""

# Summary
echo "========================================"
echo "Configuration Summary"
echo "========================================"
echo "Service Port:     ${TAXCORE_SERVICE_PORT:-3001}"
echo "Tax API URL:      ${TAXCORE_TAX_URL:-https://api.sandbox.vms.frcs.org.fj/api}"
echo "VSDC API URL:     ${TAXCORE_VSDC_URL:-http://devesdc.sandbox...}"
echo "Certificate:      ${TAXCORE_PFX_PATH:-Auto-discover}"
echo "Debug Mode:       ${TAXCORE_DEBUG:-true}"
echo ""

# Ask to start service
read -p "Start TaxCore service now? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo ""
    echo "========================================"
    echo "Starting TaxCore Service..."
    echo "========================================"
    echo "Press Ctrl+C to stop the service"
    echo ""
    sleep 2
    
    # Start the service
    node taxcore-service.js
else
    echo ""
    echo "To start the service manually, run:"
    echo "  cd websocket-server"
    echo "  npm run taxcore"
    echo ""
    echo "Or use PM2 for production:"
    echo "  pm2 start websocket-server/taxcore-service.js --name taxcore"
    echo ""
    echo "After starting, test the service:"
    echo "  curl http://localhost:${TAXCORE_SERVICE_PORT:-3001}/health"
    echo ""
fi
