# syntax=docker/dockerfile:1.7

#############################################################
# Builder
#############################################################
FROM node:22.18.0-bookworm-slim AS builder

WORKDIR /app

ENV CI=true \
    NODE_ENV=development \
    NPM_CONFIG_UPDATE_NOTIFIER=false \
    DATABASE_URL=postgres://medusa:medusa@127.0.0.1:5432/medusa \
    REDIS_URL=redis://127.0.0.1:6379 \
    STORE_CORS=http://localhost:8000 \
    ADMIN_CORS=http://localhost:7001 \
    AUTH_CORS=http://localhost:7001 \
    JWT_SECRET=build-only-jwt-secret \
    COOKIE_SECRET=build-only-cookie-secret \
    S3_FILE_URL=http://localhost:9000/ari-shopping \
    S3_ACCESS_KEY_ID=build-access-key \
    S3_SECRET_ACCESS_KEY=build-secret-key \
    S3_BUCKET=ari-shopping \
    S3_ENDPOINT=http://127.0.0.1:9000

COPY package.json package-lock.json .npmrc ./

RUN --mount=type=cache,id=medusa-npm-cache,target=/root/.npm \
    npm ci \
      --legacy-peer-deps \
      --prefer-offline \
      --no-audit \
      --no-fund

COPY tsconfig.json ./
COPY medusa-config.ts ./
COPY src ./src

RUN npm run build

#
# Elimina todas las dependencias de desarrollo
#
RUN npm prune --omit=dev

#
# Limpia cache de npm
#
RUN npm cache clean --force

#############################################################
# Runtime
#############################################################
FROM node:22.18.0-bookworm-slim

WORKDIR /app

ENV NODE_ENV=production \
    PORT=9000 \
    NPM_CONFIG_UPDATE_NOTIFIER=false

#
# Solo lo necesario para ejecutar Medusa
#
COPY --from=builder --chown=node:node /app/node_modules ./node_modules
COPY --from=builder --chown=node:node /app/.medusa/server ./

USER node

EXPOSE 9000

CMD ["node","node_modules/@medusajs/cli/dist/index.js","start"]