# syntax=docker/dockerfile:1

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

ARG IOP_CONTROL_PLANE_HTTP_URL=http://localhost:18000
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 path dependencies declared in pubspec.yaml resolve under /workspace.
# Copy manifests first so pub get can cache, then copy full sources before build.
COPY proto-socket/dart/pubspec.yaml /workspace/proto-socket/dart/
COPY agent-shell/pubspec.yaml /workspace/agent-shell/
COPY nexo/packages/messaging_flutter/pubspec.yaml /workspace/nexo/packages/messaging_flutter/
COPY iop/packages/flutter/iop_console/pubspec.yaml /workspace/iop/packages/flutter/iop_console/
COPY iop/apps/client/pubspec.yaml iop/apps/client/pubspec.lock ./
RUN flutter pub get

COPY proto-socket/dart/ /workspace/proto-socket/dart/
COPY agent-shell/ /workspace/agent-shell/
COPY nexo/packages/messaging_flutter/ /workspace/nexo/packages/messaging_flutter/
COPY iop/packages/flutter/iop_console/ /workspace/iop/packages/flutter/iop_console/
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;"]
