# Portable SPAPS local runtime image.
# Built from bundled assets shipped inside the npm package.

FROM python:3.12-slim AS base

WORKDIR /app

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        libpq-dev \
        gcc \
        curl \
    && rm -rf /var/lib/apt/lists/*

ARG SPAPS_SERVER_QUICKSTART_VERSION=0.5.1
RUN pip install --no-cache-dir "spaps-server-quickstart==${SPAPS_SERVER_QUICKSTART_VERSION}"
RUN python - <<'PY'
from pathlib import Path

from spaps_server_quickstart.domains.auth import schemas
from spaps_server_quickstart.middleware import local_mode

schema_path = Path(schemas.__file__)
text = schema_path.read_text(encoding="utf-8")
text = text.replace(
    "    refresh_token: str\n",
    "    refresh_token: str | None = None\n",
)
schema_path.write_text(text, encoding="utf-8")

local_mode_path = Path(local_mode.__file__)
text = local_mode_path.read_text(encoding="utf-8")
text = text.replace(
    'default_factory=lambda: {"issue_reporting_required_capability": "view_products"}',
    'default_factory=lambda: {"issue_reporting_required_capability": "view_products", "issue_reporting_input_modes": ["text", "voice"]}',
)
if '"issue_reporting_input_modes"' not in text:
    raise RuntimeError("Failed to patch local-mode issue reporting input modes")
local_mode_path.write_text(text, encoding="utf-8")
PY

COPY alembic.ini /app/alembic.ini
COPY alembic/ /app/alembic/
COPY scripts/ /app/scripts/

ENV APP_ROOT=/app
ENV PYTHONUNBUFFERED=1

EXPOSE 8000

ENTRYPOINT ["/app/scripts/container-entrypoint.sh"]
CMD ["uvicorn", "spaps_server_quickstart.spaps_app:app", "--host", "0.0.0.0", "--port", "8000"]
