# =========================
# Stage 1: Build Flutter web
# =========================
FROM ghcr.io/cirruslabs/flutter:3.41.2 AS builder

WORKDIR /app

# Copy only pubspec first to leverage caching
COPY pubspec.* ./
RUN flutter pub get

# Copy the rest of the source code
COPY . .

# Build Flutter web
RUN flutter build web

# =========================
# Stage 2: Serve with Nginx on port 4006
# =========================
FROM nginx:alpine

# Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*

# Copy built Flutter web files
COPY --from=builder /app/build/web /usr/share/nginx/html

# Expose port 4006
EXPOSE 4006

# Use a minimal nginx config to listen on 4006
COPY nginx.conf /etc/nginx/conf.d/default.conf

# Start Nginx in foreground
CMD ["nginx", "-g", "daemon off;"]