# syntax=docker/dockerfile:1

# Stage 1: Build custom sign-in UI
FROM --platform=$BUILDPLATFORM node:22-alpine AS build
ARG REGISTRY_TOKEN
WORKDIR /ui
COPY ui/sign-in/package.json ui/sign-in/package-lock.json ui/sign-in/.npmrc ./
RUN --mount=type=cache,target=/root/.npm echo "//gitea.siegeln.net/api/packages/cameleer/npm/:_authToken=${REGISTRY_TOKEN}" >> .npmrc && npm ci
COPY ui/sign-in/ .
RUN npm run build

# Stage 2: Logto with sign-in UI + bootstrap
FROM ghcr.io/logto-io/logto:latest

# Install bootstrap dependencies (curl, jq for API calls; postgresql16-client for DB reads)
RUN apk add --no-cache curl jq postgresql16-client

# Install all official Logto connectors (email, SMS, social — configured at runtime via vendor UI)
RUN cd /etc/logto/packages/core && npm run cli connector add -- --official 2>/dev/null || true

# Custom sign-in UI
COPY --from=build /ui/dist/ /etc/logto/packages/experience/dist/

# Bootstrap scripts
COPY docker/logto-bootstrap.sh /scripts/logto-bootstrap.sh
COPY docker/cameleer-logto/logto-entrypoint.sh /scripts/entrypoint.sh
RUN chmod +x /scripts/*.sh

ENTRYPOINT ["/scripts/entrypoint.sh"]
