#!/bin/bash
# =============================================================================
# Aurora Log Viewer
# =============================================================================
# Usage: ./logs.sh [app|db|all] [-f|--follow]
# Default: app container, no follow (shows last 100 lines)
#
# Examples:
#   ./logs.sh              # Last 100 lines of app logs
#   ./logs.sh -f           # Follow app logs in real-time
#   ./logs.sh db -f        # Follow db logs in real-time
#   ./logs.sh all           # Last 100 lines of all logs
# =============================================================================

SERVICE="app"
FOLLOW=""
TAIL_LINES="--tail=100"

# Parse arguments (order-independent)
for arg in "$@"; do
  case $arg in
    -f|--follow)
      FOLLOW="-f"
      TAIL_LINES=""  # When following, start from current output
      ;;
    app|db|all)
      SERVICE="$arg"
      ;;
    -*)
      echo "Unknown option: $arg"
      echo "Usage: ./logs.sh [app|db|all] [-f|--follow]"
      exit 1
      ;;
  esac
done

echo "📋 Fetching logs for service: $SERVICE..."

case $SERVICE in
  app)
    docker compose logs $FOLLOW $TAIL_LINES app
    ;;
  db)
    docker compose logs $FOLLOW $TAIL_LINES db
    ;;
  all)
    docker compose logs $FOLLOW $TAIL_LINES
    ;;
  *)
    echo "Unknown service: $SERVICE"
    echo "Usage: ./logs.sh [app|db|all] [-f|--follow]"
    exit 1
    ;;
esac
