# Development Dockerfile for Frontend (Flutter Web)

FROM dart:stable AS web_builder

RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    curl \
    unzip \
    xz-utils \
    libglu1-mesa \
    ca-certificates \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

RUN useradd -ms /bin/bash flutteruser

ENV PATH="/home/flutteruser/flutter/bin:${PATH}"

USER flutteruser
WORKDIR /home/flutteruser

# Install Flutter
RUN git clone --branch stable --depth 1 https://github.com/flutter/flutter.git \
  && flutter config --enable-web \
  && flutter precache --web \
  && flutter upgrade

COPY --chown=flutteruser:flutteruser frontend /home/flutteruser/app

# Replace production config with dev config
WORKDIR /home/flutteruser/app/pshared
RUN cp lib/config/common_dev.dart lib/config/common.dart

# Build shared package with code generation
RUN flutter clean \
  && flutter pub get \
  && dart run build_runner build --delete-conflicting-outputs

# Build the web client
WORKDIR /home/flutteruser/app/pweb
RUN flutter clean \
  && flutter pub get \
  && flutter build web --release --no-tree-shake-icons

# Runtime stage with Caddy
FROM caddy:alpine AS runtime

WORKDIR /usr/share/pweb

COPY --from=web_builder /home/flutteruser/app/pweb/build/web /usr/share/pweb

# Copy Caddy config (will be mounted from host)
# COPY frontend/pweb/caddy/Caddyfile /etc/caddy/Caddyfile

EXPOSE 80

CMD ["caddy", "run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
