# Stage 1: build dependencies
FROM public.ecr.aws/docker/library/python:3.13-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir --target /app/deps -r requirements.txt

# Stage 2: runtime
FROM public.ecr.aws/docker/library/python:3.13-slim
WORKDIR /app

# git powers the ontology management store (versioning / history / diff / revert)
RUN apt-get update && apt-get install -y --no-install-recommends git \
  && rm -rf /var/lib/apt/lists/*

# Copy installed packages
COPY --from=builder /app/deps /usr/local/lib/python3.13/site-packages/

# Copy application code and sample ontology data
COPY src/ ./src/
COPY input/ ./input/
# Local knowledge-graph data (ontology + policy/claim named graphs)
COPY output/ ./output/

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

# Run as non-root in Choreo/Kubernetes environments
RUN addgroup --system --gid 10001 appgroup \
  && adduser --system --uid 10001 --ingroup appgroup --home /home/appuser appuser \
  && chown -R appuser:appgroup /app

# The .env is NOT baked in - passed at runtime via env vars or secrets
# COPY .env .env

EXPOSE 8000

USER 10001

# Health check - hit the root page
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/')" || exit 1

# Run the server
CMD ["python", "src/main.py", "--port", "8000", "--skip-reasoner", "--no-browser"]
