# Use Node.js 22 Alpine image to match package.json engines requirement
FROM node:22-alpine

WORKDIR /app

# Install system dependencies
RUN apk add --no-cache openssl curl python3 make g++

# Install pnpm
RUN npm install -g pnpm

# Copy package files
COPY package.json pnpm-lock.yaml* ./

# Install all dependencies
RUN pnpm install --frozen-lockfile

# Copy Prisma schema
COPY prisma ./prisma/

# Generate Prisma client
RUN pnpm prisma generate

# Copy source code and build
COPY . .
RUN pnpm build

# Remove dev dependencies to reduce image size
ENV CI=true
RUN pnpm prune --prod

# Generate Prisma client after prune (prisma CLI now available in production deps)
RUN pnpm prisma generate

# Create uploads directory
RUN mkdir -p /app/uploads

# Set environment variables
ENV NODE_ENV=production
ENV PRISMA_ENGINES_CHECKSUM_IGNORE_MISSING=1

# Expose port
EXPOSE 3001

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD curl -f http://localhost:3001/api/health || exit 1

# Start the application
CMD ["pnpm", "start"]
