#!/bin/bash
# Script pour visualiser les logs Veza
# Usage: bash scripts/view_logs.sh [service] [options]
#
# Services disponibles:
#   backend-api, redis, db, rabbitmq, chat-server, stream-server, all
#
# Options:
#   -f, --follow     Suivre les logs en temps réel (tail -f)
#   -e, --errors     Afficher uniquement les erreurs
#   -n NUM           Nombre de lignes à afficher (défaut: 50)
#   -g PATTERN       Filtrer par pattern (grep)

set -e

LOG_DIR="/var/log/veza"
SERVICE="${1:-all}"
FOLLOW=false
ERRORS_ONLY=false
LINES=50
GREP_PATTERN=""

# Parser les options
shift || true
while [[ $# -gt 0 ]]; do
    case $1 in
        -f|--follow)
            FOLLOW=true
            shift
            ;;
        -e|--errors)
            ERRORS_ONLY=true
            shift
            ;;
        -n)
            LINES="$2"
            shift 2
            ;;
        -g)
            GREP_PATTERN="$2"
            shift 2
            ;;
        *)
            echo "Option inconnue: $1"
            exit 1
            ;;
    esac
done

# Fonction pour afficher les logs d'un fichier
view_log() {
    local file="$1"
    local label="$2"
    
    if [ ! -f "$file" ]; then
        echo "⚠️  Fichier $file non trouvé"
        return
    fi
    
    echo "📋 === $label ==="
    
    if [ "$FOLLOW" = true ]; then
        if [ -n "$GREP_PATTERN" ]; then
            tail -f "$file" | grep --color=auto "$GREP_PATTERN"
        else
            tail -f "$file"
        fi
    else
        if [ -n "$GREP_PATTERN" ]; then
            tail -n "$LINES" "$file" | grep --color=auto "$GREP_PATTERN"
        else
            tail -n "$LINES" "$file"
        fi
    fi
    echo ""
}

# Déterminer quels fichiers afficher
case $SERVICE in
    backend-api)
        if [ "$ERRORS_ONLY" = true ]; then
            view_log "$LOG_DIR/backend-api-error.log" "Backend API - Erreurs"
        else
            view_log "$LOG_DIR/backend-api.log" "Backend API - Tous les logs"
        fi
        ;;
    redis)
        if [ "$ERRORS_ONLY" = true ]; then
            view_log "$LOG_DIR/redis-error.log" "Redis - Erreurs"
        else
            view_log "$LOG_DIR/redis.log" "Redis - Tous les logs"
        fi
        ;;
    db)
        if [ "$ERRORS_ONLY" = true ]; then
            view_log "$LOG_DIR/db-error.log" "Database - Erreurs"
        else
            view_log "$LOG_DIR/db.log" "Database - Tous les logs"
        fi
        ;;
    rabbitmq)
        if [ "$ERRORS_ONLY" = true ]; then
            view_log "$LOG_DIR/rabbitmq-error.log" "RabbitMQ - Erreurs"
        else
            view_log "$LOG_DIR/rabbitmq.log" "RabbitMQ - Tous les logs"
        fi
        ;;
    chat-server)
        if [ "$ERRORS_ONLY" = true ]; then
            view_log "$LOG_DIR/chat-server-error.log" "Chat Server - Erreurs"
        else
            view_log "$LOG_DIR/chat-server.log" "Chat Server - Tous les logs"
        fi
        ;;
    stream-server)
        if [ "$ERRORS_ONLY" = true ]; then
            view_log "$LOG_DIR/stream-server-error.log" "Stream Server - Erreurs"
        else
            view_log "$LOG_DIR/stream-server.log" "Stream Server - Tous les logs"
        fi
        ;;
    all)
        echo "📊 === Vue d'ensemble des logs Veza ==="
        echo ""
        if [ "$ERRORS_ONLY" = true ]; then
            view_log "$LOG_DIR/backend-api-error.log" "Backend API - Erreurs"
            view_log "$LOG_DIR/redis-error.log" "Redis - Erreurs"
            view_log "$LOG_DIR/db-error.log" "Database - Erreurs"
            view_log "$LOG_DIR/rabbitmq-error.log" "RabbitMQ - Erreurs"
            view_log "$LOG_DIR/chat-server-error.log" "Chat Server - Erreurs"
            view_log "$LOG_DIR/stream-server-error.log" "Stream Server - Erreurs"
        else
            echo "💡 Astuce: Utilisez -e pour voir uniquement les erreurs"
            echo ""
            ls -lh "$LOG_DIR"/*.log 2>/dev/null || echo "Aucun fichier de log trouvé"
        fi
        ;;
    *)
        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 (vue d'ensemble)"
        exit 1
        ;;
esac
