FROM node:18-alpine AS base

# Install Lua 5.3
RUN apk add --no-cache lua5.3 lua5.3-dev

WORKDIR /app

# Copy package files first for better caching
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Copy application code
COPY . .

# Create temp directory
RUN mkdir -p temp && chmod 777 temp

# Expose port (Railway sets PORT env var)
EXPOSE 3000

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD node -e \\"require('http').get('http://localhost:3000/api/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})\\"

# Start server
CMD [\\"node\\", \\"server.js\\"]

