# LEAP FOOTBALL GROUP — production image
FROM node:22-bookworm-slim AS base
WORKDIR /app
ENV NEXT_TELEMETRY_DISABLED=1

FROM base AS deps
COPY package.json package-lock.json ./
RUN npm ci

FROM base AS build
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Make sure every photograph exists (restores from the text backup if binaries were dropped),
# then fail the build if any referenced image is still missing or corrupt.
RUN node scripts/restore-images.mjs && node scripts/verify-images.mjs
# DATABASE_URL is only needed so the db module can be imported during the build.
ARG DATABASE_URL=postgresql://postgres:postgres@db:5432/app_db
ENV DATABASE_URL=$DATABASE_URL
ARG NEXT_PUBLIC_SITE_URL=https://www.leapfootballgroup.com
ENV NEXT_PUBLIC_SITE_URL=$NEXT_PUBLIC_SITE_URL
RUN npm run build

FROM base AS runner
ENV NODE_ENV=production
ENV PORT=3000
COPY --from=build /app/package.json /app/package-lock.json ./
COPY --from=build /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/.next ./.next
COPY --from=build /app/public ./public
COPY --from=build /app/next.config.ts ./next.config.ts
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
  CMD node -e "fetch('http://127.0.0.1:'+(process.env.PORT||3000)+'/api/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"
CMD ["npm", "run", "start"]
