FROM node:18-alpine as build-stage

WORKDIR /app

COPY package*.json ./
# Use NPM Mirror
RUN npm config set registry https://registry.npmmirror.com
RUN npm install

COPY . .

# Build args
ARG VITE_API_BASE_URL=/api/v1
ENV VITE_API_BASE_URL=$VITE_API_BASE_URL

# Build for production
RUN npm run build

# Production Stage with Nginx
FROM nginx:stable-alpine as production-stage

# Install openssl and inotify-tools for dynamic SSL reloading
RUN apk add --no-cache openssl inotify-tools

COPY --from=build-stage /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

EXPOSE 80 443
ENTRYPOINT ["/entrypoint.sh"]

# Development Stage
FROM node:18-alpine as dev-stage

WORKDIR /app

COPY package*.json ./
# Use NPM Mirror
RUN npm config set registry https://registry.npmmirror.com
RUN npm install

COPY . .

# We will run 'vite' in development mode (or 'vite preview' for prod-like serving, 
# but Uvicorn user usually implies a simpler dev-like setup or just Python preference).
# Since user asked "Why not use Uvicorn for frontend", strictly speaking Uvicorn is Python server.
# Frontend (Vue) is JS. We cannot run Vue with Uvicorn directly.
# BUT, we can use Vite's preview or dev server.

# Expose Vite's default port
EXPOSE 5173

# Host 0.0.0.0 is needed for Docker
CMD ["npm", "run", "dev", "--", "--host", "0.0.0.0"]
