# Stage 1: Build frontend
FROM node:20-alpine AS frontend-builder

WORKDIR /app/frontend

# Copy frontend package files
COPY frontend/package.json frontend/pnpm-lock.yaml* ./
RUN npm install

# Copy frontend source
COPY frontend/ ./

# Build frontend
RUN npm run build

# Stage 2: Production
FROM node:20-alpine

WORKDIR /app

# Install docker CLI for docker compose commands
RUN apk add --no-cache docker-cli docker-cli-compose git curl unzip bash openssh-client

# Configure git global identity to avoid "Author identity unknown" errors
RUN git config --global user.email "admin@santinova-soft.org" && \
    git config --global user.name "Manus Dashboard"

# Copy backend
COPY backend/package.json ./backend/
WORKDIR /app/backend
RUN npm install --production

COPY backend/ ./

# Copy built frontend
WORKDIR /app
COPY --from=frontend-builder /app/frontend/dist ./frontend/dist

WORKDIR /app/backend

EXPOSE 3001

CMD ["node", "src/index.js"]
