#!/bin/bash
# =========================================================
# INSTALL PYTHON DEPENDENCIES - IDS System
# =========================================================
# Installa tutte le dipendenze Python necessarie per IDS

set -e

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

IDS_DIR="/opt/ids"
VENV_DIR="${IDS_DIR}/python_ml/venv"

echo -e "${BLUE}🐍 Installazione Dipendenze Python per IDS${NC}\n"

# Check se script è eseguito da root
if [ "$EUID" -ne 0 ]; then 
   echo -e "${RED}❌ Questo script deve essere eseguito come root (usa sudo)${NC}"
   exit 1
fi

# Installa python3-venv se non presente
echo -e "${BLUE}📦 Verifica python3-venv...${NC}"
if ! rpm -q python3.11-pip &>/dev/null; then
    echo -e "${YELLOW}⚙️  Installazione python3.11-pip...${NC}"
    dnf install -y python3.11-pip
fi

# Crea virtual environment
echo -e "${BLUE}🔧 Creazione virtual environment...${NC}"
if [ -d "$VENV_DIR" ]; then
    echo -e "${YELLOW}♻️  Virtual environment già esistente, rimuovo...${NC}"
    rm -rf "$VENV_DIR"
fi

python3.11 -m venv "$VENV_DIR"
echo -e "${GREEN}✅ Virtual environment creato in ${VENV_DIR}${NC}"

# Attiva virtual environment e installa dipendenze
echo -e "${BLUE}📥 Installazione dipendenze Python...${NC}"
source "${VENV_DIR}/bin/activate"

# Upgrade pip
pip install --upgrade pip

# Installa dipendenze principali
pip install fastapi==0.104.1
pip install uvicorn[standard]==0.24.0
pip install pydantic==2.5.0
pip install python-dotenv==1.0.0
pip install psycopg2-binary==2.9.9
pip install pandas==2.1.3
pip install numpy==1.26.2
pip install scikit-learn==1.3.2
pip install httpx==0.25.1
pip install joblib==1.3.2

echo -e "${GREEN}✅ Dipendenze Python installate${NC}"

# Cambia ownership a utente ids
echo -e "${BLUE}🔐 Impostazione permessi...${NC}"
chown -R ids:ids "$VENV_DIR"

# Crea directory models per salvataggio modelli ML
echo -e "${BLUE}📁 Creazione directory models...${NC}"
mkdir -p "${IDS_DIR}/python_ml/models"
chown -R ids:ids "${IDS_DIR}/python_ml/models"
chmod 755 "${IDS_DIR}/python_ml/models"
echo -e "${GREEN}✅ Directory models configurata${NC}"

# Verifica installazione
echo -e "\n${BLUE}🔍 Verifica installazione:${NC}"
source "${VENV_DIR}/bin/activate"
python3 -c "import fastapi; print(f'✅ FastAPI: {fastapi.__version__}')"
python3 -c "import uvicorn; print(f'✅ Uvicorn: {uvicorn.__version__}')"
python3 -c "import sklearn; print(f'✅ Scikit-learn: {sklearn.__version__}')"
python3 -c "import pandas; print(f'✅ Pandas: {pandas.__version__}')"
python3 -c "import httpx; print(f'✅ HTTPX: {httpx.__version__}')"
python3 -c "import joblib; print(f'✅ Joblib: {joblib.__version__}')"

echo -e "\n${GREEN}╔═══════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║   ✅ DIPENDENZE PYTHON INSTALLATE             ║${NC}"
echo -e "${GREEN}╚═══════════════════════════════════════════════╝${NC}"

echo -e "\n${BLUE}📝 NOTA:${NC}"
echo -e "   Il virtual environment è in: ${YELLOW}${VENV_DIR}${NC}"
echo -e "   I systemd services useranno automaticamente questo venv"
echo ""
