# The target operating model agent: the Execution engine and this Workflow.
#
# The engine comes from a named build context, so no file is copied by hand:
#
#   podman build --build-context engine=../orchestration_engine -t tom-analysis .
#
# The agent itself is not installed: the Workflow finds its Skills, role files
# and provider bundles beside its own code (`ROOT` in workflow.py), so it runs
# from /app as the reference agent does.

FROM python:3.12-slim AS builder

WORKDIR /engine
COPY --from=engine pyproject.toml ./
COPY --from=engine execution_engine/ ./execution_engine/
RUN pip install --no-cache-dir --prefix=/install .


FROM python:3.12-slim

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONPATH=/app \
    A2A_HOST=0.0.0.0 \
    A2A_PORT=8080 \
    CHECKPOINTER=sqlite \
    CHECKPOINT_DB_PATH=/data/checkpoints.db \
    SQLITE_FALLBACK_DB=/data/agent_local.db \
    ENV_FILE_EXPORT=false

WORKDIR /app

RUN groupadd -g 10001 appgroup && \
    useradd -u 10001 -g appgroup -s /bin/sh -m appuser

COPY --from=builder /install /usr/local

COPY app/ ./app/
COPY agents/ ./agents/
COPY providers/ ./providers/
COPY skills/ ./skills/

RUN mkdir -p deliverables lessons /data && chown -R appuser:appgroup /app /data

USER appuser
VOLUME ["/data"]
EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
  CMD python -c "import os, urllib.request; urllib.request.urlopen(f'http://localhost:{os.environ[\"A2A_PORT\"]}/health')" || exit 1

CMD ["python", "-m", "app.main"]
