# Stage 1: Build Flutter Web Application
FROM ghcr.io/cirruslabs/flutter:stable AS builder

WORKDIR /app

# Build arguments for dynamic API URL and subpath base href
ARG BASE_HREF=/kifi/
ARG API_BASE_URL=https://app.technobeesolutions.in/api/kifi-v2
ENV API_BASE_URL=${API_BASE_URL}

# Copy dependency definitions first to leverage Docker layer caching
COPY pubspec.yaml pubspec.lock ./
RUN flutter pub get

# Copy source code and assets
COPY assets ./assets
COPY lib ./lib
COPY web ./web

# Build Flutter Web release bundle with specified base href
RUN flutter build web --release --no-tree-shake-icons --base-href ${BASE_HREF} --dart-define=API_BASE_URL=${API_BASE_URL}

# Stage 2: Serve with Nginx
FROM nginx:alpine

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

# Copy custom nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf

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

EXPOSE 80

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