FROM node:24-bookworm-slim

WORKDIR /app

ENV ONNXRUNTIME_NODE_INSTALL_CUDA=skip

RUN set -eux; \
    . /etc/os-release; \
    codename="${VERSION_CODENAME:-bookworm}"; \
    printf 'Types: deb\nURIs: http://mirrors.aliyun.com/debian\nSuites: %s %s-updates\nComponents: main\nSigned-By: /usr/share/keyrings/debian-archive-keyring.gpg\n\nTypes: deb\nURIs: http://mirrors.aliyun.com/debian-security\nSuites: %s-security\nComponents: main\nSigned-By: /usr/share/keyrings/debian-archive-keyring.gpg\n' "$codename" "$codename" "$codename" > /etc/apt/sources.list.d/debian.sources; \
    apt-get update && \
    apt-get install -y --no-install-recommends ca-certificates python3 make g++ && \
    rm -rf /var/lib/apt/lists/* && \
    npm config set registry https://registry.npmmirror.com/ && \
    yarn config set registry https://registry.npmmirror.com/

COPY package.json yarn.lock ./

# The container runs the web/API service only, so strip Electron-only packages
# before installing to avoid downloading desktop binaries in CI.
RUN node -e "const fs=require('fs');const pkg=JSON.parse(fs.readFileSync('package.json','utf8'));for(const section of ['dependencies','devDependencies']){if(!pkg[section]) continue;for(const name of ['custom-electron-titlebar','electron','electron-builder','electron-rebuild','electronmon']) delete pkg[section][name];}fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2)+'\n');" && \
    yarn install --frozen-lockfile && \
    yarn cache clean

COPY . .

RUN yarn build && \
    mkdir -p /opt/toonflow-data && \
    cp -a data/. /opt/toonflow-data/ && \
    chmod +x scripts/docker-entrypoint.sh

ENV NODE_ENV=prod
ENV PORT=10588

EXPOSE 10588

ENTRYPOINT ["scripts/docker-entrypoint.sh"]
CMD ["node", "data/serve/app.js"]
