ARG BASE_IMAGE=investibook-backend-base:latest
FROM oven/bun:1.2.22-debian AS agent-runtime-builder
WORKDIR /runtime/pi-core
COPY agentkit/runtime/pi-core/package.json agentkit/runtime/pi-core/bun.lock ./
COPY agentkit/runtime/pi-core/index.mjs agentkit/runtime/pi-core/bundle-entry.mjs ./
RUN bun install --frozen-lockfile --ignore-scripts && bun run build

WORKDIR /runtime/ya-opencode-telemetry
COPY agentkit/plugins/ya-opencode-telemetry/package.json ./
COPY agentkit/plugins/ya-opencode-telemetry/index.ts ./
RUN bun run build

WORKDIR /runtime/ya-opencode-code-artifact
COPY agentkit/plugins/ya-opencode-code-artifact/package.json ./
COPY agentkit/plugins/ya-opencode-code-artifact/index.ts ./
RUN bun install --ignore-scripts && bun run build

FROM ${BASE_IMAGE}

WORKDIR /app

# Install the Investibook-pinned opencode binary fetched by CI.
COPY backend/vendor/opencode/bin/opencode /usr/local/bin/opencode
RUN chmod +x /usr/local/bin/opencode && opencode --version

# Pi runs as a Bun-compiled standalone executable. The full Pi coding-agent
# CLI/TUI and its node_modules tree are intentionally not installed.
COPY --from=agent-runtime-builder /runtime/pi-core/dist/pi-core /usr/local/bin/pi-core
RUN chmod +x /usr/local/bin/pi-core

# ============= Backend App =============
# Copy backend dependency files
COPY backend/pyproject.toml .
COPY backend/uv.lock .

# Install backend dependencies using uv
RUN uv sync --no-dev

# Copy backend application code
COPY backend/main.py .
COPY backend/investibook/ ./investibook/
COPY backend/services/ ./services/
# Bake templates into the image so workspaces can be seeded even without a
# pre-populated PVC (TEMPLATES_DIR defaults to /app/templates at runtime).
COPY backend/templates/ ./templates/
# Bake evaluation registry/baselines into the image so batch runners can load
# suites without relying on repo checkout paths.
COPY evaluation/ ./evaluation/
# Copy startup scripts
COPY backend/scripts/ ./scripts/
RUN chmod +x ./scripts/*.sh

# TriagePilot admin dashboard reads the replayer ISS registry at runtime.
COPY tools/replayer/issues/ ./tools/replayer/issues/
# Admin weekly report charts read pre-aggregated daily usage JSON at runtime.
COPY outputs/usage_daily_reports/ ./outputs/usage_daily_reports/

# ============= Agent Resources =============
# Copy agent resources (skills, configs, plugins, data) to /opt/agentkit
COPY agentkit /opt/agentkit
COPY --from=agent-runtime-builder /runtime/pi-core/dist/pi-core \
    /opt/agentkit/runtime/pi-core/dist/pi-core
COPY --from=agent-runtime-builder /runtime/ya-opencode-telemetry/dist/index.js \
    /opt/agentkit/plugins/ya-opencode-telemetry/dist/index.js
COPY --from=agent-runtime-builder /runtime/ya-opencode-code-artifact/dist/index.js \
    /opt/agentkit/plugins/ya-opencode-code-artifact/dist/index.js

# ============= Back to app directory =============
WORKDIR /app

ENV AGENTS_DIR=/opt/agentkit
ENV AGENTKIT_SKILLS_DIR=/opt/agentkit/skills
ENV AGENTKIT_PLUGINS_DIR=/opt/agentkit/plugins
ENV PANEL_DATA_DIR=/opt/agentkit/data

# Expose port
EXPOSE 8000

# Start the FastAPI server
CMD ["/app/scripts/entrypoint.sh"]
