FROM node:22-alpine AS builder

WORKDIR /app

# Copy package files
COPY neode-ui/package*.json ./

# Install all dependencies (including dev)
RUN npm install

# Copy source code
COPY neode-ui/ ./

# Clean up backup files and large unused assets before build
RUN find public/assets -name "*backup*" -type f -delete || true && \
    find public/assets -name "*1.47mb*" -type f -delete || true && \
    find public/assets -name "bg-*.mp4" -type f -delete || true

# Build the Vue app (skip type checking, just build)
ENV DOCKER_BUILD=true
ENV NODE_ENV=production

# Use npm script which handles build better
RUN npm run build:docker || (echo "Build failed! Listing files:" && ls -la && echo "Checking vite config:" && cat vite.config.ts && exit 1)

# Production stage
FROM nginx:alpine

# Copy built files to nginx
COPY --from=builder /app/dist /usr/share/nginx/html

# Copy AIUI pre-built dist
COPY demo/aiui/ /usr/share/nginx/html/aiui/

# Copy nginx config template and entrypoint
COPY neode-ui/docker/nginx-demo.conf /etc/nginx/nginx.conf.template
COPY neode-ui/docker/docker-entrypoint.sh /docker-entrypoint-custom.sh
RUN chmod +x /docker-entrypoint-custom.sh

# Expose port
EXPOSE 80

# Substitute ANTHROPIC_API_KEY at runtime, then start nginx
ENTRYPOINT ["/docker-entrypoint-custom.sh"]
