FROM python:3.11-slim

# Set working directory
WORKDIR /app

# Install system dependencies for Playwright
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    && rm -rf /var/lib/apt/lists/*

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

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

# Install Playwright browsers
RUN playwright install chromium --with-deps || true

# Create app directory structure
RUN mkdir -p /app/archival_data

# Set environment variable for archive directory (can be overridden)
ENV ARCHIVE_DIR=/app/archival_data

# Copy application code
COPY run_archiver.py .
COPY archive_engine.py .
COPY rss_processor.py .
COPY content_extractor.py .
COPY storage_manager.py .
COPY web_interface.py .
COPY scheduler.py .
COPY singlefile_archive.py .
COPY ap_processor.py .
COPY cleanup_old_files.py .
COPY rebuild_database.py .
COPY restore_database.py .
COPY rss_feeds.json .

# Copy templates and static directories if they exist
COPY templates/ ./templates/ 2>/dev/null || true
COPY static/ ./static/ 2>/dev/null || true

# Copy entrypoint script
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Expose Flask port
EXPOSE 5000

# Use entrypoint script
ENTRYPOINT ["/entrypoint.sh"]

# Default command
CMD ["python", "run_archiver.py", "--serve", "--host", "0.0.0.0", "--port", "5000"]
