# Use official Node.js LTS image
FROM node:20-alpine AS build

# Set working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY main_web/package*.json ./main_web/

# Install dependencies
RUN cd main_web && npm install && npm install -g @angular/cli

# Copy the rest of the app
COPY main_web ./main_web

# Build Angular app
RUN cd main_web && ng build --configuration=production

# --- Production image ---
FROM node:20-alpine AS prod
WORKDIR /app

# Copy built Angular app (fix path to browser output)
COPY --from=build /app/main_web/dist/main_web/browser /app/dist

# Copy server.js and minimal package.json
COPY server.js ./
COPY package.json ./

# Install only production dependencies (express)
RUN npm install --production

# Debug: list /app/dist and check for index.html
RUN ls -l /app/dist && (cat /app/dist/index.html | head -n 5) || echo 'index.html not found'

EXPOSE 8080

# Env defaults (can be overridden at runtime)
ENV DEFAULT_PROXY_TARGET=https://iko-main.ddns.net \
    ALLOWED_PROXY_TARGETS=https://iko-main.ddns.net

CMD ["node", "server.js"]
