# Use Node.js
FROM node:22-alpine

# Install curl and OpenSSL for health checks and Prisma compatibility
RUN apk add --no-cache curl openssl

# Set working directory
WORKDIR /app

# Copy package files
COPY backend/package*.json ./

# Install dependencies
RUN npm install --legacy-peer-deps

# Copy Prisma schema first (from project root)
COPY prisma ./prisma

# Generate Prisma Client
RUN npx prisma generate --schema=./prisma/schema.prisma

# Copy rest of source code
COPY backend/ .

# Normalize line endings of entrypoint script (fix CRLF on Windows checkouts)
RUN sed -i 's/\r$//' docker-entrypoint.sh

# Make entrypoint script executable
RUN chmod +x docker-entrypoint.sh

# Create logs directory
RUN mkdir -p logs

# Expose port
EXPOSE 3000

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

# Use the entrypoint script
ENTRYPOINT ["sh", "./docker-entrypoint.sh"]

