FROM node:22-alpine AS build

WORKDIR /app/frontend

COPY frontend/package*.json ./
RUN npm ci

COPY frontend/ ./

ARG VITE_API_URL=/api
ARG VITE_OIDC_ENABLED=false
ARG VITE_OIDC_ISSUER=
ARG VITE_OIDC_CLIENT_ID=
ARG VITE_OIDC_SCOPE=openid profile email
ARG VITE_OIDC_REDIRECT_URI=
ARG VITE_OIDC_POST_LOGOUT_REDIRECT_URI=
ARG VITE_DEV_API_PROXY_TARGET=http://backend:8000
ARG VITE_APP_VERSION=0.1.0
ARG VITE_APP_BUILD_INFO=dev

ENV VITE_API_URL=${VITE_API_URL} \
    VITE_OIDC_ENABLED=${VITE_OIDC_ENABLED} \
    VITE_OIDC_ISSUER=${VITE_OIDC_ISSUER} \
    VITE_OIDC_CLIENT_ID=${VITE_OIDC_CLIENT_ID} \
    VITE_OIDC_SCOPE=${VITE_OIDC_SCOPE} \
    VITE_OIDC_REDIRECT_URI=${VITE_OIDC_REDIRECT_URI} \
    VITE_OIDC_POST_LOGOUT_REDIRECT_URI=${VITE_OIDC_POST_LOGOUT_REDIRECT_URI} \
    VITE_DEV_API_PROXY_TARGET=${VITE_DEV_API_PROXY_TARGET} \
    VITE_APP_VERSION=${VITE_APP_VERSION} \
    VITE_APP_BUILD_INFO=${VITE_APP_BUILD_INFO}

RUN npm run build

FROM nginx:1.27-alpine AS prod

COPY frontend/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/frontend/dist /usr/share/nginx/html

EXPOSE 80

FROM node:22-alpine AS dev

WORKDIR /app/frontend

COPY frontend/package*.json ./
RUN npm ci

COPY frontend/ ./

ENV VITE_API_URL=/api \
    VITE_OIDC_ENABLED=false \
    VITE_DEV_API_PROXY_TARGET=http://backend:8000 \
    VITE_APP_VERSION=0.1.0 \
    VITE_APP_BUILD_INFO=dev

EXPOSE 5173
CMD ["npm", "run", "dev", "--", "--host", "0.0.0.0", "--port", "5173"]
