# ---- Base ----
FROM node:22-slim AS base
RUN corepack enable && corepack prepare pnpm@10.27.0 --activate
WORKDIR /app

# ---- Dependencies ----
FROM base AS deps
COPY pnpm-lock.yaml pnpm-workspace.yaml package.json turbo.json ./
COPY apps/web/package.json apps/web/
RUN pnpm install --frozen-lockfile --filter @goodgo/web...

# ---- Build ----
FROM base AS build
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/apps/web/node_modules ./apps/web/node_modules
COPY tsconfig.base.json ./
COPY apps/web/ apps/web/
RUN cd apps/web && npx next build

# ---- Flatten standalone ----
# Next.js standalone with pnpm creates symlinks that point outside the standalone dir.
# We copy .pnpm (real files), then fix symlinks to point to local .pnpm store.
FROM node:22-slim AS flatten
WORKDIR /app
COPY --from=build /app/apps/web/.next/standalone/ /standalone/

# Copy .pnpm store (real files) and rebuild top-level symlinks
RUN cp -r /standalone/node_modules/.pnpm /app/node_modules/.pnpm 2>/dev/null || true && \
    # For each broken symlink in standalone/node_modules, find the package in .pnpm and link it
    for link in /standalone/node_modules/*; do \
      name=$(basename "$link"); \
      [ "$name" = ".pnpm" ] && continue; \
      if [ -L "$link" ]; then \
        # Find the real package dir in .pnpm
        real=$(find /app/node_modules/.pnpm -path "*/$name/package.json" -not -path "*/node_modules/.pnpm/*" 2>/dev/null | head -1); \
        if [ -n "$real" ]; then \
          ln -sf "$(dirname "$real")" "/app/node_modules/$name"; \
        fi; \
      elif [ -d "$link" ]; then \
        cp -r "$link" "/app/node_modules/$name"; \
      fi; \
    done && \
    # Copy server files from apps/web
    cp -r /standalone/apps/web/* /app/ 2>/dev/null || true && \
    cp -r /standalone/apps/web/.next /app/.next 2>/dev/null || true && \
    rm -rf /standalone

# ---- Production ----
FROM node:22-slim AS production
RUN apt-get update && apt-get install -y --no-install-recommends dumb-init && rm -rf /var/lib/apt/lists/*
WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV HOSTNAME=0.0.0.0
ENV PORT=3000

# Copy flattened standalone
RUN mkdir -p ./public
COPY --from=flatten --chown=node:node /app ./
COPY --from=build --chown=node:node /app/apps/web/.next/static ./.next/static
# Copy public assets if any exist (may be empty)
COPY --from=build --chown=node:node /app/apps/web/public ./public

EXPOSE 3000

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD node -e "fetch('http://localhost:3000/api/health').then(r => { if (!r.ok) throw 1 }).catch(() => { process.exit(1) })"

USER node

ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "server.js"]
