# Oliver Metadata Tool - Docker Image
# Multi-stage build for optimized image size

FROM python:3.11-slim as base

# Set working directory
WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    # ExifTool - critical for metadata operations (300+ formats)
    libimage-exiftool-perl \
    # Tesseract OCR with CJK language support
    tesseract-ocr \
    tesseract-ocr-eng \
    tesseract-ocr-chi-sim \
    tesseract-ocr-chi-tra \
    tesseract-ocr-jpn \
    tesseract-ocr-kor \
    # Poppler for PDF to image conversion
    poppler-utils \
    # FFmpeg for video processing (optional)
    ffmpeg \
    # Build dependencies
    gcc \
    && rm -rf /var/lib/apt/lists/*

# Verify ExifTool installation
RUN exiftool -ver

# Copy requirements first for better layer caching
COPY requirements.txt .

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Create necessary directories
RUN mkdir -p /app/uploads /app/output /app/data /app/templates

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV DOCKER_MODE=true
ENV FLASK_APP=web_app.py

# Expose port
EXPOSE 5001

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
    CMD python -c "import requests; requests.get('http://localhost:5001/login', timeout=5)" || exit 1

# Run application with gunicorn (production WSGI server)
CMD ["gunicorn", "--bind", "0.0.0.0:5001", "--workers", "2", "--timeout", "120", "web_app:app"]
