FROM debian:latest AS build

RUN apt-get update && \
    apt-get install -y libxi6 libgtk-3-0 libxrender1 libxtst6 libxslt1.1 curl git wget unzip libgconf-2-4 gdb libstdc++6 libglu1-mesa fonts-droid-fallback lib32stdc++6 python3 && \
    apt-get clean

RUN git clone https://github.com/flutter/flutter.git /usr/local/flutter

#Set Flutter path
ENV PATH="/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}"

RUN flutter doctor -v && \
    flutter channel master && \
    flutter upgrade && \
    flutter config --enable-web

#Create app directory
RUN mkdir /app/

#Copy application files
COPY . /app/

#Set the working directory inside the container
WORKDIR /app/

#Build the Flutter web application
RUN flutter build web --release

#Stage 2: Nginx server
FROM nginx:1.27.2-alpine

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

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

#Expose port 80
EXPOSE 80

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