ARG DEBIAN_IMAGE=debian:bookworm-slim
ARG RUST_IMAGE=rust:1.75-slim-bookworm
ARG PYTHON_IMAGE=python:3.11.4-slim-bookworm

FROM ${DEBIAN_IMAGE} as downloader

ARG TARGETPLATFORM

SHELL ["/bin/bash", "-c"]

RUN apt update -y
RUN apt install -y unzip curl

RUN [ "$TARGETPLATFORM" == "linux/amd64" ] && curl -Lsf https://github.com/denoland/deno/releases/download/v1.41.0/deno-x86_64-unknown-linux-gnu.zip -o deno.zip || true
RUN [ "$TARGETPLATFORM" == "linux/arm64" ] && curl -Lsf https://github.com/denoland/deno/releases/download/v1.41.0/deno-aarch64-unknown-linux-gnu.zip -o deno.zip || true


RUN unzip deno.zip && rm deno.zip


FROM ${RUST_IMAGE} as builder

RUN apt-get update && apt-get install -y git libssl-dev pkg-config

RUN apt-get -y update \
    && apt-get install -y \
    curl

ENV SQLX_OFFLINE=true


RUN mkdir -p /frontend/build
RUN apt-get update \
    && apt-get install -y ca-certificates tzdata libpq5 cmake\
    make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev \
    libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libxml2-dev \
    libxmlsec1-dev libffi-dev liblzma-dev mecab-ipadic-utf8 libgdbm-dev libc6-dev git libprotobuf-dev libnl-route-3-dev \
    libv8-dev  nodejs npm clang libclang-dev\
    && rm -rf /var/lib/apt/lists/*


RUN wget https://golang.org/dl/go1.21.5.linux-amd64.tar.gz && tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz
ENV PATH="${PATH}:/usr/local/go/bin"
ENV GO_PATH=/usr/local/go/bin/go

ENV TZ=Etc/UTC

ENV PYTHON_VERSION 3.11.4

RUN wget https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz \
    && tar -xf Python-${PYTHON_VERSION}.tgz && cd Python-${PYTHON_VERSION}/ && ./configure --enable-optimizations \
    && make -j 4 && make install

RUN /usr/local/bin/python3 -m pip install pip-tools

COPY --from=oven/bun:1.0.29 /usr/local/bin/bun /usr/bin/bun


RUN [ "$TARGETPLATFORM" == "linux/amd64" ] && curl -Lsf https://github.com/denoland/deno/releases/download/v1.41.0/deno-x86_64-unknown-linux-gnu.zip -o deno.zip || true
RUN [ "$TARGETPLATFORM" == "linux/arm64" ] && curl -Lsf https://github.com/denoland/deno/releases/download/v1.41.0/deno-aarch64-unknown-linux-gnu.zip -o deno.zip || true

COPY --from=downloader --chmod=755 /deno /usr/bin/deno

RUN apt-get update \
    && apt-get install -y postgresql-client --allow-unauthenticated

RUN rustup component add rustfmt