# Multi-stage build for Nuxt 3 admin frontend
FROM node:20-slim as builder

WORKDIR /app

# Copy package files
COPY package*.json ./
COPY nuxt.config.ts ./
COPY tsconfig.json ./

# Install dependencies
RUN npm install --no-audit --progress=false

# Copy source code
COPY . .

# Build the application
RUN npm run build

# Production stage
FROM node:20-slim as runner

WORKDIR /app

# Create non-root user
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nuxtuser

# Copy built application and dependencies
COPY --from=builder --chown=nuxtuser:nodejs /app/.output ./
COPY --from=builder --chown=nuxtuser:nodejs /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

# Switch to non-root user
USER nuxtuser

# Expose port 3000 (Nuxt default)
EXPOSE 3000

# Start the application
ENV NUXT_HOST=0.0.0.0
ENV NUXT_PORT=3000

CMD ["node", "./server/index.mjs"]