FROM debian:trixie AS build-essential

RUN set -eux; \
  apt-get update; \
  apt-get upgrade -y

RUN set -eux; \
  apt-get update; \
  apt-get install -y --no-install-recommends \
  build-essential \
  cmake \
  checkinstall \
  ;

FROM build-essential AS build-pgdev

RUN set -eux; \
  apt-get update; \
  apt-get install -y --no-install-recommends \
  postgresql-server-dev-17 \
  ;

# =============================================================================
# pgTAP
#
FROM debian:trixie AS pgtap

# Installer les outils nécessaires pour compiler pgTAP
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        git \
        build-essential \
        ca-certificates \
        postgresql-server-dev-17 \
        make

# Cloner et compiler pgTAP
RUN git clone --depth 1 https://github.com/theory/pgtap.git /usr/src/pgtap && \
    cd /usr/src/pgtap && \
    make && \
    make install DESTDIR=/pgtap-dist

# =============================================================================
# pgRouting
#
FROM build-pgdev AS pgrouting

ARG pgrouting_release=3.8.0

ADD "https://github.com/pgRouting/pgrouting/releases/download/v${pgrouting_release}/pgrouting-${pgrouting_release}.tar.gz" \
  /tmp/pgrouting.tar.gz

RUN set -eux; \
  tar -xvf /tmp/pgrouting.tar.gz -C /tmp; \
  rm -rf /tmp/pgrouting.tar.gz;

# Installer les dépendances de compilation
RUN set -eux; \
  apt-get update; \
  apt-get install -y --no-install-recommends \
  libboost-all-dev \
  ;

WORKDIR /tmp/pgrouting-${pgrouting_release}/build
RUN cmake -DBUILD_HTML=OFF -DBUILD_DOXY=OFF ..

# Create debian package
RUN checkinstall -D --install=no --fstrans=no --backup=no --pakdir=/tmp \
  --pkgname=pgrouting --pkgversion=${pgrouting_release} \
  --nodoc

# =============================================================================
# pg_cron
#
FROM build-pgdev AS pgcron

# Télécharger
ARG pg_cron_release=1.6.7

ADD "https://github.com/citusdata/pg_cron/archive/refs/tags/v${pg_cron_release}.tar.gz" \
    /tmp/pg_cron.tar.gz

# Extraire
RUN tar -xvf /tmp/pg_cron.tar.gz -C /tmp && \
    rm -rf /tmp/pg_cron.tar.gz

# Build from source
WORKDIR /tmp/pg_cron-${pg_cron_release}
RUN make

# Create debian package
RUN checkinstall -D --install=no --fstrans=no --backup=no --pakdir=/tmp --nodoc

# =============================================================================
# pgVector
#
FROM build-pgdev AS pgvector

ARG pgvector_release=0.6.2

ADD "https://github.com/pgvector/pgvector/archive/refs/tags/v${pgvector_release}.tar.gz" \
  /tmp/pgvector.tar.gz

RUN tar -xvf /tmp/pgvector.tar.gz -C /tmp && \
  rm -rf /tmp/pgvector.tar.gz

# Build from source
WORKDIR /tmp/pgvector-${pgvector_release}
RUN make

# Create debian package
RUN checkinstall -D --install=no --fstrans=no --backup=no --pakdir=/tmp \
  --nodoc

# =============================================================================
# Étape 3 : image finale PostgreSQL propre
#
FROM postgres:17.6

RUN set -eux; \
  apt-get update; \
  apt-get install -y --no-install-recommends \
    locales; \
  sed -i '/fr_FR.UTF-8/s/^# //' /etc/locale.gen; \
  locale-gen

ENV TZ=Europe/Paris
ENV LANG=fr_FR.UTF-8
ENV LANGUAGE=fr_FR:fr
ENV LC_ALL=fr_FR.UTF-8

RUN set -eux; \
  apt-get update; \
  apt-get install -y --no-install-recommends \
    postgresql-postgis

# Copier uniquement les fichiers installés depuis le builder
COPY --from=pgtap /pgtap-dist/ /
COPY --from=pgrouting /tmp/*.deb /tmp
COPY --from=pgvector /tmp/*.deb /tmp
COPY --from=pgcron /tmp/*.deb /tmp

RUN set -eux; \
  apt-get install \
    /tmp/*.deb \
  -y; \
  rm /tmp/*.deb;
