#!/bin/bash
###############################################################################
# AutoRemaster - Environment Check Script
#
# Verifies that all dependencies and requirements are properly installed
###############################################################################

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

cd "$(dirname "$0")"

echo -e "${BLUE}╔════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║   AutoRemaster - Environment Diagnostics             ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════════════════╝${NC}"
echo ""

# Track overall status
ALL_GOOD=true

# Check 1: Virtual environment
echo -e "${BLUE}[1/6]${NC} Checking virtual environment..."
if [ -d "venv310" ]; then
    echo -e "      ${GREEN}✓${NC} venv310 directory exists"
else
    echo -e "      ${RED}✗${NC} venv310 directory NOT FOUND"
    ALL_GOOD=false
fi

# Check 2: Python binary
echo -e "${BLUE}[2/6]${NC} Checking Python installation..."
if [ -f "venv310/bin/python" ]; then
    VERSION=$(./venv310/bin/python --version 2>&1)
    echo -e "      ${GREEN}✓${NC} Python binary found: $VERSION"
else
    echo -e "      ${RED}✗${NC} Python binary NOT FOUND"
    ALL_GOOD=false
fi

# Check 3: Core dependencies
echo -e "${BLUE}[3/6]${NC} Checking core dependencies..."
PYTHON_BIN="./venv310/bin/python"

if [ -f "$PYTHON_BIN" ]; then
    DEPS_TO_CHECK=(
        "customtkinter:CustomTkinter"
        "numpy:NumPy"
        "scipy:SciPy"
        "soundfile:SoundFile"
        "matplotlib:Matplotlib"
        "pedalboard:Pedalboard"
        "pyloudnorm:PyLoudnorm"
        "pygame:Pygame"
        "demucs:Demucs"
        "torch:PyTorch"
    )

    for dep in "${DEPS_TO_CHECK[@]}"; do
        IFS=':' read -r module_name display_name <<< "$dep"
        if $PYTHON_BIN -c "import $module_name" 2>/dev/null; then
            VERSION=$($PYTHON_BIN -c "import $module_name; print(getattr($module_name, '__version__', 'unknown'))" 2>/dev/null)
            echo -e "      ${GREEN}✓${NC} $display_name ($VERSION)"
        else
            echo -e "      ${RED}✗${NC} $display_name NOT INSTALLED"
            ALL_GOOD=false
        fi
    done
fi

# Check 4: Source files
echo -e "${BLUE}[4/6]${NC} Checking application source files..."
SOURCE_FILES=(
    "src/main.py"
    "src/ui/app.py"
    "src/ui/header_frame.py"
    "src/ui/controls_frame.py"
    "src/ui/status_frame.py"
    "src/ui/waveform_frame.py"
    "src/engine/processor.py"
    "src/engine/worker.py"
    "src/engine/presets.py"
    "src/utils.py"
)

for file in "${SOURCE_FILES[@]}"; do
    if [ -f "$file" ]; then
        echo -e "      ${GREEN}✓${NC} $file"
    else
        echo -e "      ${RED}✗${NC} $file NOT FOUND"
        ALL_GOOD=false
    fi
done

# Check 5: Display environment
echo -e "${BLUE}[5/6]${NC} Checking display environment..."
if [ -n "$DISPLAY" ]; then
    echo -e "      ${GREEN}✓${NC} DISPLAY set to: $DISPLAY"
else
    echo -e "      ${YELLOW}⚠${NC} DISPLAY not set (may cause GUI issues)"
    echo -e "      ${YELLOW}→${NC} Will default to :0"
fi

# Check 6: Temp directory
echo -e "${BLUE}[6/6]${NC} Checking workspace..."
if [ -d "temp" ]; then
    TEMP_SIZE=$(du -sh temp 2>/dev/null | cut -f1)
    echo -e "      ${GREEN}✓${NC} temp directory exists (size: $TEMP_SIZE)"
else
    echo -e "      ${YELLOW}⚠${NC} temp directory missing (will be created on first run)"
fi

# Summary
echo ""
echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
if [ "$ALL_GOOD" = true ]; then
    echo -e "${GREEN}✓ All checks passed! Ready to launch.${NC}"
    echo ""
    echo -e "Run the application with:"
    echo -e "  ${BLUE}./start.sh${NC}"
else
    echo -e "${RED}✗ Some checks failed. Please fix the issues above.${NC}"
    echo ""
    echo -e "To install missing dependencies:"
    echo -e "  ${BLUE}export PATH=\"./venv310/bin:\$PATH\"${NC}"
    echo -e "  ${BLUE}pip install -r requirements.txt${NC}"
fi
echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
