FROM python:3.11-slim

# Set working directory
WORKDIR /app

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

# Copy requirements and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application files
COPY *.py ./
COPY *config.json ./

# Create output directories
RUN mkdir -p /app/snapshots /app/logs

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

# Copy cron configuration
COPY crontab /etc/cron.d/parentzone-downloader
RUN chmod 0644 /etc/cron.d/parentzone-downloader
RUN crontab /etc/cron.d/parentzone-downloader

# Create log file
RUN touch /var/log/cron.log

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app

# Expose volume for persistent data
VOLUME ["/app/snapshots", "/app/logs"]

# Start cron and keep container running
CMD ["sh", "-c", "cron && tail -f /var/log/cron.log"]
