FROM node:20-alpine AS builder

WORKDIR /app

# Install build tools for native modules
RUN apk add --no-cache python3 make g++

COPY package*.json ./
RUN npm install

COPY tsconfig.json ./
COPY src ./src
COPY scripts ./scripts

RUN npm run build

# --- Production image ---
FROM node:20-alpine

WORKDIR /app

RUN apk add --no-cache ffmpeg python3 py3-pip wget \
    && pip3 install --break-system-packages yt-dlp

COPY package*.json ./
RUN npm install --omit=dev

COPY --from=builder /app/dist ./dist
COPY --from=builder /app/scripts ./scripts
COPY src/config ./src/config

ENV NODE_ENV=production
ENV AGENT_V3_PORT=24803
ENV AGENT_V3_HOST=0.0.0.0

EXPOSE 24803

HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost:24803/api/v3/health || exit 1

CMD ["node", "dist/index.js"]
