FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive
ENV LANG=zh_CN.UTF-8
ENV LC_ALL=zh_CN.UTF-8
ENV PATH=/usr/local/bin:$PATH

# 1) 基本工具 + locales
RUN apt-get update \
 && apt-get install -y --no-install-recommends \
    ca-certificates curl gnupg lsb-release software-properties-common \
    build-essential git ripgrep locales wget xz-utils unzip pkg-config \
    libssl-dev libffi-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev \
    libncurses5-dev libncursesw5-dev liblzma-dev procps apt-transport-https \
 && sed -i 's/# zh_CN.UTF-8 UTF-8/zh_CN.UTF-8 UTF-8/' /etc/locale.gen || true \
 && locale-gen zh_CN.UTF-8 \
 && update-locale LANG=zh_CN.UTF-8 LC_ALL=zh_CN.UTF-8 \
 && rm -rf /var/lib/apt/lists/*

# 2) Node.js LTS (NodeSource)
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - \
 && apt-get update && apt-get install -y --no-install-recommends nodejs \
 && npm set progress=false \
 && npm config set fund false \
 && npm config set audit false \
 && rm -rf /var/lib/apt/lists/*

# 3) Python 3.11 (deadsnakes)
RUN add-apt-repository ppa:deadsnakes/ppa -y \
 && apt-get update \
 && apt-get install -y --no-install-recommends python3.11 python3.11-dev python3.11-venv python3.11-distutils \
 && rm -rf /var/lib/apt/lists/*

# 4) pip for python3.11
RUN curl -fsSL https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py \
 && python3.11 /tmp/get-pip.py \
 && rm -f /tmp/get-pip.py

# 5) Python 包（excel-mcp-server）及 uvicorn/uvloop
RUN python3.11 -m pip install --no-cache-dir --upgrade pip setuptools wheel \
 && python3.11 -m pip install --no-cache-dir excel-mcp-server uvicorn uvloop

# 6) pyright (Python LSP)
RUN npm install -g pyright

# 7) opencode 官方安装脚本（构建时联网运行，安装到镜像）
RUN curl -fsSL https://opencode.ai/install -o /tmp/opencode-install.sh \
 && bash /tmp/opencode-install.sh \
 && rm -f /tmp/opencode-install.sh

# 8) 在容器内为 MCP 本地实现安装 node_modules
RUN mkdir -p /root/.config/opencode/mcp/postgres /root/.config/opencode/mcp/filesystem \
 && cd /root/.config/opencode/mcp/postgres && npm init -y >/dev/null 2>&1 \
 && npm install --no-audit --no-fund @modelcontextprotocol/server-postgres@latest \
 && cd /root/.config/opencode/mcp/filesystem && npm init -y >/dev/null 2>&1 \
 && npm install --no-audit --no-fund @modelcontextprotocol/server-filesystem@latest

# Ensure config dir exists (user will mount opencode.json)
RUN mkdir -p /root/.config/opencode

# 9) copy entrypoint (用于占位符替换的可选脚本)
COPY opencode-entrypoint.sh /usr/local/bin/opencode-entrypoint.sh
RUN chmod +x /usr/local/bin/opencode-entrypoint.sh

# Entrypoint: 脚本会做 {env:VAR} 占位替换（若需要），然后 exec "$@"（默认执行 CMD）
ENTRYPOINT ["/usr/local/bin/opencode-entrypoint.sh"]
CMD ["opencode","start"]
