FROM python:3.12.3

# Set timezone
ENV TZ=Asia/Dubai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install system dependencies (including all calibre dependencies)
RUN apt update -y && \
    apt install -y python3 python3-pip supervisor imagemagick libcairo2-dev \
    libreoffice-core libreoffice-writer libreoffice-calc libreoffice-impress \
    ffmpeg p7zip-full jpegoptim pngquant npm poppler-utils potrace \
    ghostscript tesseract-ocr tesseract-ocr-eng wget \
    libopengl0 libgl1-mesa-glx libxcb-cursor0 xvfb \
    libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 \
    libxcb-render-util0 libxcb-shape0 libxcb-xfixes0 libxcb-xinerama0 \
    libxcb-xkb1 libxkbcommon-x11-0 && \
    apt clean && \
    rm -rf /var/lib/apt/lists/*

# Install calibre (with xvfb for headless mode)
RUN wget -nv -O- https://download.calibre-ebook.com/linux-installer.sh | xvfb-run sh /dev/stdin version=7.10.0

# Install svgo
RUN npm install -g svgo

# Install Python dependencies
COPY requirements.txt /website/requirements.txt
RUN pip3 install --no-cache-dir -r /website/requirements.txt && \
    pip3 install rq gunicorn

# Copy application files
WORKDIR /website
COPY . /website/


# Make start.sh executable
COPY start.sh /website/start.sh



RUN chmod +x /website/start.sh

# Create necessary directories
RUN mkdir -p /website/logs /website/uploads && \
    chmod 755 /website/logs /website/uploads

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV REDIS_HOST=redis
ENV REDIS_PORT=6379

# APP_PORT is read from .env at runtime (--env-file .env) or passed via -e APP_PORT=XXXX.
# The ARG lets you also override it at build time: docker build --build-arg APP_PORT=8080 .
ARG APP_PORT=6001
ENV APP_PORT=${APP_PORT}
EXPOSE ${APP_PORT}

CMD ["/website/start.sh"]
