# Recipe Manager Backend Dockerfile
# Multi-stage build for production

# Stage 1: Build
FROM node:22-alpine AS builder

WORKDIR /app

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

# Install all dependencies (including dev dependencies for build)
RUN npm ci

# Copy source code
COPY src ./src

# Build TypeScript
RUN npm run build

# Stage 2: Production
FROM node:22-alpine

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install production dependencies only
RUN npm ci --omit=dev

# Copy built JavaScript from builder stage
COPY --from=builder /app/dist ./dist

# Copy database schema (required by migrate script)
COPY src/backend/db/schema.sql ./src/backend/db/schema.sql

# Create data directory for SQLite database
RUN mkdir -p /app/data

# Expose API port
EXPOSE 3000

# Set environment variables
ENV NODE_ENV=production
ENV DATABASE_PATH=/app/data/recipes.db

# Run database migrations on startup, then start server
CMD ["sh", "-c", "node dist/backend/db/migrate.js && node dist/backend/index.js"]
