# PostgreSQL 18 with pgvector and vectorchord extensions
FROM postgres:18-alpine

# Install build dependencies
RUN apk add --no-cache --virtual .build-deps \
    gcc \
    musl-dev \
    postgresql-dev \
    make \
    git \
    cmake \
    clang \
    openssl-dev \
    libxml2-dev \
    libxslt-dev \
    python3 \
    py3-pip \
    build-base

# Install pgvector extension
RUN git clone --branch v0.7.3 https://github.com/pgvector/pgvector.git /tmp/pgvector \
    && cd /tmp/pgvector \
    && make \
    && make install

# Install vectorchord extension (for Immich)
RUN git clone --branch v0.3.0 https://github.com/vectorchord/pg_vectorscale.git /tmp/pg_vectorscale \
    && cd /tmp/pg_vectorscale \
    && make \
    && make install

# Clean up build dependencies
RUN apk del .build-deps \
    && rm -rf /tmp/* \
    && rm -rf /var/lib/apt/lists/*

# Configure PostgreSQL to load extensions
RUN echo "shared_preload_libraries = 'pg_vectorscale,vector'" >> /var/lib/postgresql/data/postgresql.conf

COPY init-postgres.sh /docker-entrypoint-initdb.d/

EXPOSE 5432
CMD ["postgres"]
