# ---- Build Stage ----
FROM node:23-alpine AS builder

WORKDIR /app

# Install pnpm and necessary build tools
RUN apk add --no-cache python3 make g++ pnpm

# First copy all config files
COPY tsconfig.json tsconfig.deploy.json ./
COPY package.json pnpm-lock.yaml ./

# Now copy source code
COPY src/ ./src/
COPY deploy-commands.ts ./

# Install dependencies AFTER copying config files
RUN pnpm install --frozen-lockfile

# Build the TypeScript code directly
RUN npx tsc -p tsconfig.json && npx tsc -p tsconfig.deploy.json

# ---- Production Stage ----
FROM node:23-alpine

WORKDIR /app

ENV NODE_ENV=production

# Copy application files
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/pnpm-lock.yaml ./pnpm-lock.yaml
COPY application.yml ./application.yml
COPY plugins ./plugins

# Install production dependencies only
# Temporarily disable the prepare script by setting npm_config_ignore_scripts
RUN apk add --no-cache pnpm && \
    npm_config_ignore_scripts=true pnpm install --prod --frozen-lockfile

# Run the compiled JavaScript application
CMD ["node", "dist/index.js"]
