FROM node:20-alpine AS builder
WORKDIR /app
COPY . .
RUN if [ -f package.json ]; then npm ci || npm install; fi
RUN npm run build || true
FROM node:20-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app .
RUN npm install -g serve@latest
RUN ln -s /app/.next /app/.next/server/app/_next || true
EXPOSE 3000
ENV PORT=3000
CMD ["sh", "-c", "if [ -f .next/standalone/server.js ]; then node .next/standalone/server.js; elif [ -d .next/server/app ]; then serve .next/server/app -l ${PORT:-3000}; elif [ -d public ]; then serve public -l ${PORT:-3000}; else serve . -l ${PORT:-3000}; fi"]