# Convivo Admin app — multi-stage build.
#
# Stage 1 (build): compile the Vite/React/TS app into static assets.
# Stage 2 (serve): nginx serves the static build and proxies the Supabase
#   same-origin paths (/auth, /rest, /realtime, /storage) to the Supabase core.
#
# The Supabase core itself is NOT built here — it runs via the Supabase CLI
# (`supabase start`) or as a separate container. See the root docker-compose.yml.

# ---- Build stage -------------------------------------------------------------
FROM node:20-alpine AS build
WORKDIR /app

# Install deps first for better layer caching (reproducible via lock file).
COPY package.json package-lock.json ./
RUN npm ci

# Copy sources and build. The anon key is baked in at build time (it is not a
# secret — it is the standard local Supabase anon key). Override with
# --build-arg VITE_SUPABASE_ANON_KEY=... if you use a custom key.
ARG VITE_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0
ENV VITE_SUPABASE_ANON_KEY=$VITE_SUPABASE_ANON_KEY
COPY . .
RUN npm run build

# ---- Serve stage -------------------------------------------------------------
FROM nginx:1.27-alpine AS serve
# nginx template: serve the SPA + proxy Supabase paths to the core. The proxy
# target is injected at container start (see docker-entrypoint.sh).
COPY nginx.conf.template /etc/nginx/conf.d/default.conf.template
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 3001
ENTRYPOINT ["/docker-entrypoint.sh"]
