# syntax=docker/dockerfile:1.7
FROM --platform=$BUILDPLATFORM rust:1.94-trixie AS chef

ARG BUILDARCH
ARG TARGETARCH

WORKDIR /app

RUN if [ "$TARGETARCH" = "arm64" ] && [ "$BUILDARCH" != "arm64" ]; then \
        apt-get update && apt-get install -y --no-install-recommends \
            gcc-aarch64-linux-gnu \
            g++-aarch64-linux-gnu \
            libc6-dev-arm64-cross \
            pkg-config && \
        rustup target add aarch64-unknown-linux-gnu && \
        rm -rf /var/lib/apt/lists/*; \
    elif [ "$TARGETARCH" = "amd64" ] && [ "$BUILDARCH" != "amd64" ]; then \
        apt-get update && apt-get install -y --no-install-recommends \
            gcc-x86-64-linux-gnu \
            g++-x86-64-linux-gnu \
            libc6-dev-amd64-cross \
            pkg-config && \
        rustup target add x86_64-unknown-linux-gnu && \
        rm -rf /var/lib/apt/lists/*; \
    fi

ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc \
    CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc \
    CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc \
    CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc

RUN --mount=type=cache,target=/usr/local/cargo/registry \
    cargo install cargo-chef --locked

RUN if [ "$TARGETARCH" = "$BUILDARCH" ]; then \
        echo '--release --locked' > /tmp/cargo-args && \
        echo 'target/release/pokedex-api' > /tmp/binary-path; \
    elif [ "$TARGETARCH" = "arm64" ]; then \
        echo '--release --locked --target aarch64-unknown-linux-gnu' > /tmp/cargo-args && \
        echo 'target/aarch64-unknown-linux-gnu/release/pokedex-api' > /tmp/binary-path; \
    elif [ "$TARGETARCH" = "amd64" ]; then \
        echo '--release --locked --target x86_64-unknown-linux-gnu' > /tmp/cargo-args && \
        echo 'target/x86_64-unknown-linux-gnu/release/pokedex-api' > /tmp/binary-path; \
    fi

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
RUN --mount=type=cache,target=/usr/local/cargo/registry \
    --mount=type=cache,target=/usr/local/cargo/git \
    --mount=type=cache,target=/app/target \
    cargo chef cook $(cat /tmp/cargo-args) --recipe-path recipe.json

COPY Cargo.toml Cargo.lock ./
COPY src/ src/
RUN --mount=type=cache,target=/usr/local/cargo/registry \
    --mount=type=cache,target=/usr/local/cargo/git \
    --mount=type=cache,target=/app/target \
    cargo build $(cat /tmp/cargo-args) --bin pokedex-api && \
    cp $(cat /tmp/binary-path) /usr/local/bin/pokedex-api

FROM debian:trixie-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates curl \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd --system app \
    && useradd --system --gid app --home-dir /nonexistent --shell /usr/sbin/nologin app

COPY --from=builder /usr/local/bin/pokedex-api /usr/local/bin/pokedex-api

ARG GIT_SHA=unknown
ENV GIT_SHA=$GIT_SHA \
    BIND_ADDR=0.0.0.0:8000

EXPOSE 8000
USER app
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD curl -fsS http://127.0.0.1:8000/health || exit 1

CMD ["pokedex-api"]
