# Multi-stage build for production
FROM oven/bun:1.1.0 AS builder
WORKDIR /app

# Install dependencies
COPY package.json bun.lock ./
RUN bun install

# Production stage  
FROM oven/bun:1.1.0 AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV PORT=8080

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

# Copy node_modules from builder
COPY --from=builder /app/node_modules ./node_modules

# Copy application files
COPY public ./public
COPY src ./src
COPY package.json ./

# Expose port
EXPOSE 8080

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
  CMD bun -e "fetch('http://localhost:8080/api/settings').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))" || exit 1

# Start server
CMD ["bun", "run", "src/server/index.ts"]