# ──────────────────────────────────────────────────────────────
# arelle-mcp test environment
# Multi-stage: slim base + Arelle + MCP SDK + test tooling
# ──────────────────────────────────────────────────────────────

FROM python:3.12-slim AS base

LABEL maintainer="King Hippopotamus"
LABEL description="arelle-mcp test environment with Arelle XBRL processor"

# System deps for lxml (Arelle dependency)
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    libxml2-dev \
    libxslt1-dev \
    curl \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# ── Stage 1: Install Python deps (cached layer) ─────────────
FROM base AS deps

COPY pyproject.toml ./

# Install all deps including dev extras
RUN pip install --no-cache-dir \
    arelle-release>=2.30 \
    "mcp>=1.9" \
    httpx>=0.25 \
    pydantic>=2.0 \
    pytest>=8.0 \
    pytest-asyncio>=0.23 \
    pytest-timeout>=2.2 \
    pytest-cov>=5.0 \
    ruff>=0.4

# ── Stage 2: Full test image ────────────────────────────────
FROM deps AS test

# Copy source
COPY src/ ./src/
COPY tests/ ./tests/
COPY pyproject.toml ./

# Install package in editable mode
RUN pip install --no-cache-dir -e ".[dev]"

# Pre-warm: verify Arelle imports work
RUN python -c "from arelle.api.Session import Session; print('✓ Arelle Session OK')" && \
    python -c "from arelle.RuntimeOptions import RuntimeOptions; print('✓ RuntimeOptions OK')" && \
    python -c "from arelle import ModelXbrl; print('✓ ModelXbrl OK')" && \
    python -c "from arelle import XbrlConst; print('✓ XbrlConst OK')"

# Env
ENV PYTHONPATH=/app/src
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV ARELLE_MCP_LOG_LEVEL=DEBUG

# ── Entrypoints ─────────────────────────────────────────────

# Default: run all tests
CMD ["pytest", "tests/", "-v", "--tb=short", "--timeout=120"]

# ── Stage 3: Prod-slim (no test deps) ───────────────────────
FROM base AS prod

COPY src/ ./src/
COPY pyproject.toml ./

RUN pip install --no-cache-dir \
    arelle-release>=2.30 \
    "mcp>=1.9" \
    httpx>=0.25 \
    pydantic>=2.0 \
    && pip install --no-cache-dir -e .

ENV PYTHONPATH=/app/src
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

EXPOSE 8000

ENTRYPOINT ["arelle-mcp"]
