FROM python:3.12-bookworm

WORKDIR /home/app/web

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt-get update && \
    apt-get install -y --no-install-recommends gcc netcat-openbsd \
    libpq-dev \
    build-essential && \
    rm -rf /var/lib/apt/lists/*

COPY requirements.txt /home/app/web/requirements.txt

# Install Python deps
RUN pip install --upgrade pip && pip install -r /home/app/web/requirements.txt

# Copy the project
COPY . /home/app/web/

# Create logs dir AFTER copy, pre-create all files the logger will need
# (including the .lock files concurrent_log_handler creates automatically)
RUN mkdir -p /home/app/web/app/logs && \
    touch /home/app/web/app/logs/app_log.log.jsonl && \
    touch /home/app/web/app/logs/.__app_log.log.jsonl.lock && \
    touch /home/app/web/app/logs/performance_log.log && \
    touch /home/app/web/app/logs/.__performance_log.lock

# Create user and fix ownership
RUN addgroup --system app && adduser --system --group app
RUN chown -R app:app /home/app/web
# Give full rwx on the logs directory so the app user can create new lock files
RUN chmod -R 775 /home/app/web/app/logs

USER app

CMD ["python", "run.py"]
