FROM node:20-bookworm

WORKDIR /usr/src/app

# Install only needed build deps, then clean up
RUN apt-get update && apt-get install -y python3 make g++ && rm -rf /var/lib/apt/lists/*

ENV NODE_ENV=development
ENV NPM_CONFIG_PRODUCTION=false
ENV PATH="/usr/src/app/node_modules/.bin:${PATH}"
# dummy DB URL for prisma generate at build time
ENV DATABASE_URL=postgresql://user:pass@localhost:5432/papo?schema=public
ENV PRISMA_IGNORE_ENV_LOAD=true

# Install backend dependencies
COPY package*.json ./
RUN npm ci --include=dev

# Install frontend dependencies
COPY frontend/package*.json ./frontend/
RUN npm --prefix frontend ci

# Copy source
COPY . .

# Build frontend
RUN npm run build:web

# Ensure prisma CLI available globally (avoids path issues)
RUN npm install -g prisma@5.4.2

# Generate Prisma client (explicit schema path)
RUN prisma generate --schema=src/database/schema.prisma

# Build backend (tsc emits JS even with type errors; exit code suppressed for pre-existing errors)
RUN npm run build:web && npx tsc || true

# Optional: show versions in build log
RUN node -v && npm -v && npx prisma -v

CMD ["npm", "start"]
