#!/bin/bash

# Attribute Agent - Stop Script
# This script stops both backend and frontend services

PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
PID_FILE="$PROJECT_DIR/.pids"

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

echo -e "${YELLOW}Stopping Attribute Agent...${NC}"

# Kill processes from PID file
if [ -f "$PID_FILE" ]; then
    while read -r pid; do
        if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
            echo "Stopping process $pid..."
            kill "$pid" 2>/dev/null
        fi
    done < "$PID_FILE"
    rm -f "$PID_FILE"
fi

# Also kill any remaining uvicorn and vite processes for this project
pkill -f "uvicorn app.main:app" 2>/dev/null
pkill -f "vite.*novelty-seeking" 2>/dev/null

echo -e "${GREEN}All services stopped.${NC}"
