#!/bin/bash
# Smart Video Encoder Launcher - Linux/WSL
# Usage: ./run_smart_encoder.sh [options]

set -e

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
MAGENTA='\033[0;35m'
RESET='\033[0m'

# Configuration
TV_DIR="${TV_DIR:-/mnt/z/tv}"
CONTENT_DIR="${CONTENT_DIR:-/mnt/z/content}"
JOBS="${JOBS:-1}"
TV_ONLY=false
CONTENT_ONLY=false

# Parse arguments
while [[ $# -gt 0 ]]; do
    case "$1" in
        --tv-dir)
            TV_DIR="$2"
            shift 2
            ;;
        --content-dir)
            CONTENT_DIR="$2"
            shift 2
            ;;
        --jobs)
            JOBS="$2"
            shift 2
            ;;
        --tv-only)
            TV_ONLY=true
            shift
            ;;
        --content-only)
            CONTENT_ONLY=true
            shift
            ;;
        --help|-h)
            echo "Usage: $0 [options]"
            echo ""
            echo "Options:"
            echo "  --tv-dir <path>       TV directory (default: /mnt/z/tv)"
            echo "  --content-dir <path>  Content directory (default: /mnt/z/content)"
            echo "  --jobs <count>        Parallel jobs (default: 1)"
            echo "  --tv-only             Process TV directory only"
            echo "  --content-only        Process content directory only"
            echo "  --help, -h            Show this help"
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            echo "Use --help for usage"
            exit 1
            ;;
    esac
done

log_info() {
    echo -e "${CYAN}$*${RESET}"
}

log_success() {
    echo -e "${GREEN}✓ $*${RESET}"
}

log_error() {
    echo -e "${RED}❌ $*${RESET}"
}

log_warn() {
    echo -e "${YELLOW}⚠️  $*${RESET}"
}

# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PYTHON_SCRIPT="$SCRIPT_DIR/smart_encoder.py"

# Check dependencies
log_info "Checking dependencies..."

if ! command -v python3 &> /dev/null; then
    log_error "Python 3 not found"
    log_warn "Install with: sudo apt install python3"
    exit 1
fi

if ! command -v ffmpeg &> /dev/null; then
    log_error "FFmpeg not found"
    log_warn "Install with: sudo apt install ffmpeg"
    exit 1
fi

if ! command -v ffprobe &> /dev/null; then
    log_error "FFprobe not found"
    log_warn "Install with: sudo apt install ffmpeg"
    exit 1
fi

if ! command -v ab-av1 &> /dev/null; then
    log_error "ab-av1 not found"
    log_warn "Install with: cargo install ab-av1"
    exit 1
fi

log_success "All dependencies found"

# Check if script exists
if [[ ! -f "$PYTHON_SCRIPT" ]]; then
    log_error "smart_encoder.py not found at: $PYTHON_SCRIPT"
    exit 1
fi

# Make script executable
chmod +x "$PYTHON_SCRIPT"

# Print banner
echo ""
echo -e "${MAGENTA}============================================================${RESET}"
echo -e "${MAGENTA}🎬 Smart Video Encoder - Linux/WSL${RESET}"
echo -e "${MAGENTA}============================================================${RESET}"
echo ""

# Print configuration
log_info "Configuration:"
echo "   TV Directory: $TV_DIR"
echo "   Content Directory: $CONTENT_DIR"
echo "   Parallel Jobs: $JOBS"

if [[ "$TV_ONLY" == true ]]; then
    echo -e "   ${YELLOW}Mode: TV only${RESET}"
elif [[ "$CONTENT_ONLY" == true ]]; then
    echo -e "   ${YELLOW}Mode: Content only${RESET}"
else
    echo -e "   ${YELLOW}Mode: TV + Content${RESET}"
fi

# Check directories
if [[ "$TV_ONLY" == false ]] && [[ ! -d "$TV_DIR" ]]; then
    log_warn "TV directory not found: $TV_DIR"
fi

if [[ "$CONTENT_ONLY" == false ]] && [[ ! -d "$CONTENT_DIR" ]]; then
    log_warn "Content directory not found: $CONTENT_DIR"
fi

# Build command
CMD="python3 $PYTHON_SCRIPT --tv-dir $TV_DIR --content-dir $CONTENT_DIR --jobs $JOBS"

if [[ "$TV_ONLY" == true ]]; then
    CMD="$CMD --tv-only"
elif [[ "$CONTENT_ONLY" == true ]]; then
    CMD="$CMD --content-only"
fi

# Run
echo ""
log_info "Starting encoder..."
echo -e "${CYAN}============================================================${RESET}"
echo ""

eval $CMD
EXIT_CODE=$?

echo ""
echo -e "${CYAN}============================================================${RESET}"

if [[ $EXIT_CODE -eq 0 ]]; then
    log_success "Encoding completed successfully"
else
    log_error "Encoding failed with exit code: $EXIT_CODE"
fi

echo -e "${CYAN}============================================================${RESET}"

# Show log locations
echo ""
log_info "Log files:"
echo "   $HOME/Videos/encodes/logs/tv.jsonl"
echo "   $HOME/Videos/encodes/logs/content.jsonl"
echo "   $HOME/Videos/encodes/logs/rejected.jsonl"
echo "   $HOME/Videos/encodes/logs/metadata.jsonl"
echo ""

exit $EXIT_CODE
