#!/bin/bash

# Women Safety App - Stop All Services Script

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo -e "${YELLOW}🛑 Stopping Women Safety App Services${NC}"

# Stop microservices
echo -e "${YELLOW}Stopping microservices...${NC}"

if [ -f "user_service.pid" ]; then
    kill $(cat user_service.pid) 2>/dev/null
    rm user_service.pid
    echo -e "${GREEN}✅ User Service stopped${NC}"
fi

if [ -f "emergency_service.pid" ]; then
    kill $(cat emergency_service.pid) 2>/dev/null
    rm emergency_service.pid
    echo -e "${GREEN}✅ Emergency Service stopped${NC}"
fi

if [ -f "location_service.pid" ]; then
    kill $(cat location_service.pid) 2>/dev/null
    rm location_service.pid
    echo -e "${GREEN}✅ Location Service stopped${NC}"
fi

if [ -f "calendar_service.pid" ]; then
    kill $(cat calendar_service.pid) 2>/dev/null
    rm calendar_service.pid
    echo -e "${GREEN}✅ Calendar Service stopped${NC}"
fi

if [ -f "notification_service.pid" ]; then
    kill $(cat notification_service.pid) 2>/dev/null
    rm notification_service.pid
    echo -e "${GREEN}✅ Notification Service stopped${NC}"
fi

if [ -f "nutrition_service.pid" ]; then
    kill "$(cat nutrition_service.pid)" 2>/dev/null
    rm nutrition_service.pid
    echo -e "${GREEN}✅ Nutrition Service stopped${NC}"
fi

if [ -f "api_gateway.pid" ]; then
    kill "$(cat api_gateway.pid)" 2>/dev/null
    rm api_gateway.pid
    echo -e "${GREEN}✅ API Gateway stopped${NC}"
fi

# Stop infrastructure services
echo -e "${YELLOW}Stopping infrastructure services...${NC}"
docker-compose down

echo -e "${GREEN}🏁 All services stopped successfully!${NC}"