ARG POSTGRES_VERSION=18.3
ARG PGVECTOR_IMAGE=pgvector/pgvector:0.8.3-pg18
ARG PG_TEXTSEARCH_VERSION=1.3.1

FROM ${PGVECTOR_IMAGE} AS pgvector
FROM postgres:${POSTGRES_VERSION}-trixie

ARG TARGETARCH
ARG PG_TEXTSEARCH_VERSION

COPY --from=pgvector /usr/lib/postgresql/18/lib/vector.so /usr/lib/postgresql/18/lib/vector.so
COPY --from=pgvector /usr/share/postgresql/18/extension/vector.control /usr/share/postgresql/18/extension/vector.control
COPY --from=pgvector /usr/share/postgresql/18/extension/vector--*.sql /usr/share/postgresql/18/extension/

RUN set -eux; \
    case "$TARGETARCH" in \
      amd64) pg_textsearch_arch=amd64 ;; \
      arm64) pg_textsearch_arch=arm64 ;; \
      *) echo "Unsupported architecture: $TARGETARCH" >&2; exit 1 ;; \
    esac; \
    apt-get update; \
    apt-get install -y --no-install-recommends ca-certificates curl unzip; \
    curl -fsSL \
      "https://github.com/timescale/pg_textsearch/releases/download/v${PG_TEXTSEARCH_VERSION}/pg-textsearch-v${PG_TEXTSEARCH_VERSION}-pg18-${pg_textsearch_arch}.zip" \
      -o /tmp/pg_textsearch.zip; \
    unzip /tmp/pg_textsearch.zip -d /tmp/pg_textsearch; \
    apt-get install -y --no-install-recommends \
      "/tmp/pg_textsearch/pg-textsearch-postgresql-18_${PG_TEXTSEARCH_VERSION}-1_${pg_textsearch_arch}.deb"; \
    rm -rf /var/lib/apt/lists/* /tmp/pg_textsearch /tmp/pg_textsearch.zip

CMD ["postgres", "-c", "shared_preload_libraries=pg_textsearch"]
