# Trading Portal — Frontend Dockerfile
# Multi-stage: Vite build → Nginx serve

# ---- Build stage ----
FROM node:20-alpine AS build

WORKDIR /app

# Copy package files
COPY package.json package-lock.json ./
RUN npm ci

# Copy source and build
COPY . .
RUN npm run build

# ---- Serve stage ----
FROM nginx:1.27-alpine

# Copy built assets
COPY --from=build /app/dist /usr/share/nginx/html

# Fix permissions so nginx can read all files
RUN chmod -R 644 /usr/share/nginx/html/*.json /usr/share/nginx/html/*.js /usr/share/nginx/html/*.html /usr/share/nginx/html/*.svg && \
    chmod -R 644 /usr/share/nginx/html/assets/* && \
    find /usr/share/nginx/html -type d -exec chmod 755 {} \;

# Copy nginx config (full http wrapper with rate limiting)
COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD curl -sf http://localhost:80/ || exit 1

CMD ["nginx", "-g", "daemon off;"]
