#!/bin/bash
# Script pour gérer la rotation manuelle des logs Veza
# Usage: bash scripts/rotate_logs.sh [service]
#
# Services disponibles:
#   backend-api, redis, db, rabbitmq, chat-server, stream-server, all

set -e

LOG_DIR="/var/log/veza"
SERVICE="${1:-all}"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

rotate_file() {
    local file="$1"
    local label="$2"
    
    if [ ! -f "$file" ]; then
        echo "⚠️  Fichier $file non trouvé, ignoré"
        return
    fi
    
    local size=$(du -h "$file" | cut -f1)
    echo "🔄 Rotation de $label ($size)..."
    
    # Créer une copie avec timestamp
    cp "$file" "${file}.${TIMESTAMP}"
    
    # Compresser l'ancien fichier
    gzip "${file}.${TIMESTAMP}"
    
    # Vider le fichier actuel
    > "$file"
    
    echo "✅ $label roté vers ${file}.${TIMESTAMP}.gz"
}

case $SERVICE in
    backend-api)
        rotate_file "$LOG_DIR/backend-api.log" "Backend API"
        rotate_file "$LOG_DIR/backend-api-error.log" "Backend API Errors"
        ;;
    redis)
        rotate_file "$LOG_DIR/redis.log" "Redis"
        rotate_file "$LOG_DIR/redis-error.log" "Redis Errors"
        ;;
    db)
        rotate_file "$LOG_DIR/db.log" "Database"
        rotate_file "$LOG_DIR/db-error.log" "Database Errors"
        ;;
    rabbitmq)
        rotate_file "$LOG_DIR/rabbitmq.log" "RabbitMQ"
        rotate_file "$LOG_DIR/rabbitmq-error.log" "RabbitMQ Errors"
        ;;
    chat-server)
        rotate_file "$LOG_DIR/chat-server.log" "Chat Server"
        rotate_file "$LOG_DIR/chat-server-error.log" "Chat Server Errors"
        ;;
    stream-server)
        rotate_file "$LOG_DIR/stream-server.log" "Stream Server"
        rotate_file "$LOG_DIR/stream-server-error.log" "Stream Server Errors"
        ;;
    all)
        echo "🔄 Rotation de tous les logs Veza..."
        echo ""
        rotate_file "$LOG_DIR/backend-api.log" "Backend API"
        rotate_file "$LOG_DIR/backend-api-error.log" "Backend API Errors"
        rotate_file "$LOG_DIR/redis.log" "Redis"
        rotate_file "$LOG_DIR/redis-error.log" "Redis Errors"
        rotate_file "$LOG_DIR/db.log" "Database"
        rotate_file "$LOG_DIR/db-error.log" "Database Errors"
        rotate_file "$LOG_DIR/rabbitmq.log" "RabbitMQ"
        rotate_file "$LOG_DIR/rabbitmq-error.log" "RabbitMQ Errors"
        rotate_file "$LOG_DIR/chat-server.log" "Chat Server"
        rotate_file "$LOG_DIR/chat-server-error.log" "Chat Server Errors"
        rotate_file "$LOG_DIR/stream-server.log" "Stream Server"
        rotate_file "$LOG_DIR/stream-server-error.log" "Stream Server Errors"
        echo ""
        echo "✅ Rotation terminée!"
        ;;
    *)
        echo "❌ Service inconnu: $SERVICE"
        echo ""
        echo "Services disponibles:"
        echo "  - backend-api"
        echo "  - redis"
        echo "  - db"
        echo "  - rabbitmq"
        echo "  - chat-server"
        echo "  - stream-server"
        echo "  - all"
        exit 1
        ;;
esac

echo ""
echo "📊 Fichiers de logs actuels:"
ls -lh "$LOG_DIR"/*.log 2>/dev/null || echo "Aucun fichier actif"
echo ""
echo "📦 Archives compressées:"
ls -lh "$LOG_DIR"/*.gz 2>/dev/null || echo "Aucune archive"
