FROM ghcr.io/cirruslabs/flutter:stable AS build
WORKDIR /app
ARG TELEGRAM_BOT_USERNAME="carfteebot"
ARG BUILD_VERSION="local"
COPY pubspec.* ./
RUN flutter pub get
COPY . .
RUN rm -rf build/web && \
  flutter build web --release --base-href=/ -o build/web \
  --dart-define=TELEGRAM_BOT_USERNAME="$TELEGRAM_BOT_USERNAME" && \
  cp web/index.prod.html build/web/index.html && \
  rm -f build/web/index.prod.html && \
  dart run sw:generate \
    --input=build/web \
    --prefix=mapflow \
    --version="$BUILD_VERSION"

FROM nginx:1.27-alpine
RUN apk add --no-cache curl jq
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY scripts/40-mapflow-runtime-config.sh /docker-entrypoint.d/40-mapflow-runtime-config.sh
COPY --from=build /app/build/web /usr/share/nginx/html
RUN chmod +x /docker-entrypoint.d/40-mapflow-runtime-config.sh
EXPOSE 80
