FROM node:20-alpine

RUN apk add --no-cache openssl ca-certificates curl

WORKDIR /app

RUN addgroup --system --gid 1001 nodejs && adduser --system --uid 1001 nextjs

# Copy standalone build (pre-built outside Docker)
COPY .next/standalone/falah-mobile ./
COPY .next/static ./.next/static
COPY public ./public
COPY prisma/schema.prisma ./prisma/schema.prisma

# Fix Turbopack's hashed Prisma client path
# Turbopack resolves @prisma/client to @prisma/client-<hash> but the
# build-time symlink points to a path that doesn't exist in Docker.
# We copy the existing @prisma/client to the hash name so Node.js
# can resolve it when the Turbopack runtime requires it.
RUN if [ -d ".next/node_modules/@prisma" ]; then \
      for d in .next/node_modules/@prisma/client-*; do \
        name=$(basename "$d"); \
        rm -f "node_modules/@prisma/$name" 2>/dev/null; \
        cp -r node_modules/@prisma/client "node_modules/@prisma/$name"; \
      done; \
    fi

RUN mkdir -p /app/data

USER nextjs
EXPOSE 3000

ENV NODE_ENV=production
ENV DATABASE_URL="file:/app/data/dev.db"

CMD ["node", "server.js"]
