#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# Install dependencies for all copied repositories

set -e

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📦 Installing Dependencies for All Repositories"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

BASE_DIR="/Users/sandhya/UCD/ECS_DDS/graphq-llm"

# ResLens
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "1. Installing ResLens dependencies..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [ -d "$BASE_DIR/ResLens" ]; then
    cd "$BASE_DIR/ResLens"
    if [ -d "node_modules" ]; then
        echo "✅ ResLens dependencies already installed"
    else
        echo "Installing..."
        npm install
        echo "✅ ResLens dependencies installed"
    fi
else
    echo "⚠️  ResLens directory not found, skipping..."
fi
echo ""

# ResLens-Middleware
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "2. Installing ResLens-Middleware dependencies..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [ -d "$BASE_DIR/ResLens-Middleware/middleware" ]; then
    cd "$BASE_DIR/ResLens-Middleware/middleware"
    if [ -d "node_modules" ]; then
        echo "✅ ResLens-Middleware dependencies already installed"
    else
        echo "Installing..."
        npm install
        echo "✅ ResLens-Middleware dependencies installed"
    fi
elif [ -d "$BASE_DIR/ResLens-Middleware" ]; then
    cd "$BASE_DIR/ResLens-Middleware"
    if [ -d "node_modules" ]; then
        echo "✅ ResLens-Middleware dependencies already installed"
    else
        echo "Installing..."
        npm install
        echo "✅ ResLens-Middleware dependencies installed"
    fi
else
    echo "⚠️  ResLens-Middleware directory not found, skipping..."
fi
echo ""

# Nexus
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "3. Installing Nexus dependencies..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [ -d "$BASE_DIR/nexus" ]; then
    cd "$BASE_DIR/nexus"
    if [ -d "node_modules" ]; then
        echo "✅ Nexus dependencies already installed"
    else
        echo "Installing..."
        npm install
        echo "✅ Nexus dependencies installed"
    fi
else
    echo "⚠️  Nexus directory not found, skipping..."
fi
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ Dependency Installation Complete!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "Next steps:"
echo "  1. Run ./fix-reslens.sh to start all services"
echo "  2. Or manually start each service:"
echo "     - ResLens: cd ResLens && npm run dev"
echo "     - Middleware: cd ResLens-Middleware/middleware && PORT=3003 npm start"
echo "     - Nexus: cd nexus && PORT=3002 npm run dev"
echo ""

