FROM node:24-bookworm-slim AS frontend-build

WORKDIR /src/src/Eis.Web/ClientApp
COPY src/Eis.Web/ClientApp/package.json src/Eis.Web/ClientApp/package-lock.json ./
RUN npm ci
COPY src/Eis.Web/ClientApp/ ./
RUN npm run build

FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build

WORKDIR /src
COPY . .
COPY --from=frontend-build /src/src/Eis.Web/wwwroot/vue-app ./src/Eis.Web/wwwroot/vue-app
RUN dotnet restore src/Eis.Web/Eis.Web.csproj \
    && dotnet restore src/Eis.Tools/Eis.Tools.csproj \
    && dotnet publish src/Eis.Web/Eis.Web.csproj --configuration Release --output /app/web --no-restore \
    && dotnet publish src/Eis.Tools/Eis.Tools.csproj --configuration Release --output /app/tools --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS runtime

WORKDIR /app
RUN apt-get update \
    && apt-get install --yes --no-install-recommends curl \
    && rm -rf /var/lib/apt/lists/*
COPY --from=build --chown=$APP_UID:$APP_UID /app/web /app/web
COPY --from=build --chown=$APP_UID:$APP_UID /app/tools /app/tools
RUN mkdir -p /app/data \
    && chown -R $APP_UID:$APP_UID /app/data

ENV ASPNETCORE_ENVIRONMENT=Production \
    ASPNETCORE_URLS=http://0.0.0.0:4173 \
    DATABASE_CLIENT=sqlite \
    SQLITE_PATH=/app/data/exam.sqlite

EXPOSE 4173
VOLUME ["/app/data"]

USER $APP_UID
WORKDIR /app/web

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
  CMD ["curl", "--fail", "--silent", "--show-error", "http://127.0.0.1:4173/health/live"]

ENTRYPOINT ["dotnet", "Eis.Web.dll"]
