# Use Python 3.11 slim image
FROM python:3.11-slim

# Set working directory
WORKDIR /app

# Default scraper interval (24 hours in seconds)
ENV SCRAPER_INTERVAL_SECONDS=86400

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

# Copy requirements file
COPY auto_workflow/requirements.txt .

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

# Copy source code
COPY auto_workflow/src/ ./src/

# Copy environment file
COPY auto_workflow/.env .

# Create periodic execution script
RUN printf '%s\n' \
    '#!/bin/bash' \
    '' \
    '# Get interval from environment variable, default to 24 hours' \
    'INTERVAL=${SCRAPER_INTERVAL_SECONDS:-86400}' \
    '' \
    'echo "Scraper interval set to ${INTERVAL} seconds"' \
    '' \
    'while true; do' \
    '    echo "========================================"' \
    '    echo "Running scraper at $(date)"' \
    '    echo "========================================"' \
    '    python /app/src/scraper.py' \
    '    echo "Sleeping for ${INTERVAL} seconds..."' \
    '    sleep ${INTERVAL}' \
    'done' \
    > /app/run_periodic.sh \
    && chmod +x /app/run_periodic.sh

# Default command - run the periodic script
CMD ["/bin/bash", "/app/run_periodic.sh"]
