FROM python:3.12-slim

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libldap2-dev \
    libsasl2-dev \
    gosu \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy requirements and install
COPY backend/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Create non-root user
RUN adduser --system --group appuser

# Copy application files
COPY backend ./backend

# Copy initialization scripts (shared with start_server.sh)
COPY scripts ./scripts

# We define the data dir explicitly for Docker
ENV DATA_DIR="/app/data"
ENV LOGS_DIR="/app/logs"

# Pre-create directories and ensure they are writable
RUN mkdir -p /app/data /app/logs && chown -R appuser:appuser /app

# Make initialization scripts executable
RUN chmod +x /app/scripts/init_data.sh /app/backend/entrypoint.sh

EXPOSE 8000

EXPOSE 8000

# Entrypoint runs init_data.sh first, then starts uvicorn
ENTRYPOINT ["/app/backend/entrypoint.sh"]
