# syntax=docker/dockerfile:1

FROM ghcr.io/cirruslabs/flutter:stable AS builder

ARG IOP_CONTROL_PLANE_HTTP_URL=http://localhost:9080
ARG IOP_CONTROL_PLANE_WIRE_URL=ws://localhost:19080/client

WORKDIR /workspace/iop/apps/client

# docker-compose builds this Dockerfile with /config/workspace as context so the
# sibling proto-socket/dart path dependency declared in pubspec.yaml resolves to
# /workspace/proto-socket/dart inside the image. Copy its manifest first so pub
# get can cache, then copy full source before the build step.
COPY proto-socket/dart/pubspec.yaml proto-socket/dart/pubspec.lock /workspace/proto-socket/dart/
COPY iop/apps/client/pubspec.yaml iop/apps/client/pubspec.lock ./
RUN flutter pub get

COPY proto-socket/dart/ /workspace/proto-socket/dart/
COPY iop/apps/client/ ./
RUN flutter build web \
    --release \
    --dart-define=IOP_CONTROL_PLANE_HTTP_URL=$IOP_CONTROL_PLANE_HTTP_URL \
    --dart-define=IOP_CONTROL_PLANE_WIRE_URL=$IOP_CONTROL_PLANE_WIRE_URL

FROM nginx:1.27-alpine AS runner

COPY --from=builder /workspace/iop/apps/client/build/web /usr/share/nginx/html
COPY iop/apps/client/nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 3000

CMD ["nginx", "-g", "daemon off;"]
