#!/bin/bash

# Load environment variables
if [ ! -f ../.env ]; then
    echo "Error: .env file not found!"
    exit 1
fi

source ../.env

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

# Function to show usage
show_usage() {
    echo "Usage: $0 [options] [service]"
    echo ""
    echo "Options:"
    echo "  -f, --follow      Follow log output"
    echo "  -n, --lines N     Show last N lines (default: 100)"
    echo "  -e, --errors      Show only error messages"
    echo "  -w, --warnings    Show error and warning messages"
    echo "  -s, --since TIME  Show logs since timestamp (e.g. 42m for 42 minutes)"
    echo "  -u, --until TIME  Show logs before timestamp"
    echo "  -h, --help        Show this help message"
    echo ""
    echo "Services:"
    echo "  wordpress         Show WordPress logs"
    echo "  db               Show MySQL logs"
    echo "  all              Show all logs (default)"
    echo ""
    echo "Examples:"
    echo "  $0                        # Show last 100 lines of all logs"
    echo "  $0 -f wordpress           # Follow WordPress logs"
    echo "  $0 -n 50 -e db           # Show last 50 lines of MySQL errors"
    echo "  $0 -s 1h -w              # Show warnings and errors from last hour"
}

# Default values
FOLLOW=false
LINES=100
ERRORS_ONLY=false
WARNINGS=false
SINCE=""
UNTIL=""
SERVICE="all"

# Parse command line options
while [[ $# -gt 0 ]]; do
    case "$1" in
        -f|--follow)
            FOLLOW=true
            shift
            ;;
        -n|--lines)
            LINES="$2"
            shift 2
            ;;
        -e|--errors)
            ERRORS_ONLY=true
            shift
            ;;
        -w|--warnings)
            WARNINGS=true
            shift
            ;;
        -s|--since)
            SINCE="--since $2"
            shift 2
            ;;
        -u|--until)
            UNTIL="--until $2"
            shift 2
            ;;
        -h|--help)
            show_usage
            exit 0
            ;;
        wordpress|db|all)
            SERVICE="$1"
            shift
            ;;
        *)
            echo "Error: Unknown option '$1'"
            show_usage
            exit 1
            ;;
    esac
done

# Build filter command
FILTER=""
if [ "$ERRORS_ONLY" = true ]; then
    FILTER="grep -i 'error\\|exception\\|fatal'"
elif [ "$WARNINGS" = true ]; then
    FILTER="grep -i 'error\\|exception\\|fatal\\|warning'"
fi

# Build docker-compose command
CMD="docker-compose logs"
[ "$FOLLOW" = true ] && CMD="$CMD -f"
[ -n "$LINES" ] && CMD="$CMD --tail=$LINES"
[ -n "$SINCE" ] && CMD="$CMD $SINCE"
[ -n "$UNTIL" ] && CMD="$CMD $UNTIL"

# Add service filter
case "$SERVICE" in
    wordpress|db)
        CMD="$CMD $SERVICE"
        ;;
    all)
        # Service remains unspecified to show all
        ;;
    *)
        echo "Error: Unknown service '$SERVICE'"
        show_usage
        exit 1
        ;;
esac

# Execute command with optional filtering
if [ -n "$FILTER" ]; then
    eval "$CMD | $FILTER"
else
    eval "$CMD"
fi 