#!/bin/bash
# =========================================================
# CRON TRAINING - Addestramento automatico modello ML
# =========================================================
# Esegue training ogni 12 ore con 100K log più recenti
# =========================================================

# Logging
LOG_FILE="/var/log/ids/training.log"
mkdir -p /var/log/ids
exec >> "$LOG_FILE" 2>&1

echo "========================================="
echo "🤖 [$(date)] TRAINING AUTOMATICO AVVIATO"
echo "========================================="

# Esegue training via API
curl -X POST http://localhost:8000/train \
  -H "Content-Type: application/json" \
  -d '{
    "max_records": 100000,
    "hours_back": 24,
    "contamination": 0.01
  }' \
  --max-time 300

EXIT_CODE=$?

if [ $EXIT_CODE -eq 0 ]; then
    echo "✅ [$(date)] Training completato con successo"
else
    echo "❌ [$(date)] Training fallito (exit code: $EXIT_CODE)"
fi

echo ""
