# Stage 1: Build frontend
FROM node:22-alpine AS build-frontend
WORKDIR /app/frontend
COPY frontend/package*.json ./
RUN npm install
COPY frontend/ ./
RUN npx vite build

# Stage 2: Build backend
FROM node:22-alpine AS build-backend
WORKDIR /app/backend
COPY backend/package*.json ./
RUN npm install
COPY backend/ ./
RUN npm run build

# Stage 3: Production
FROM node:22-alpine AS production
WORKDIR /app

# Install only production deps for backend
COPY backend/package*.json ./backend/
RUN cd backend && npm install --omit=dev

# Copy compiled backend
COPY --from=build-backend /app/backend/dist ./backend/dist

# Copy built frontend into location the backend will serve
COPY --from=build-frontend /app/frontend/dist ./frontend/dist

# Data directory for SQLite
RUN mkdir -p /app/data

EXPOSE 3000

ENV NODE_ENV=production

CMD ["node", "backend/dist/index.js"]
