FROM node:20-slim

# Install dependencies including Google Cloud SDK prerequisites
RUN apt-get update && apt-get install -y \
    git \
    curl \
    build-essential \
    python3 \
    python3-pip \
    sudo \
    ca-certificates \
    gnupg \
    lsb-release \
    && rm -rf /var/lib/apt/lists/*

# Install Google Cloud SDK
RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
    curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg && \
    apt-get update && apt-get install -y google-cloud-cli && \
    rm -rf /var/lib/apt/lists/*

# Create a non-root user (claude does not allow `bypassPermissions` as root)
RUN useradd -m -s /bin/bash claude && \
    echo "claude ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# Install Claude Code CLI
RUN npm install -g @anthropic-ai/claude-code || echo "Failed to install claude-code"

# Verify installation
RUN which claude-code || echo "claude-code not found in PATH"
RUN ls -la /usr/local/bin/ | grep claude || echo "No claude binaries found"

# Install TypeScript tools
RUN npm install -g ts-node typescript tsx

# Copy everything to maintain structure
COPY . /app
WORKDIR /app

# Create workspace and set ownership
# RUN mkdir -p /workspace && chown -R claude:claude /workspace
# RUN chown -R claude:claude /app/template-app-1
# WORKDIR /workspace

# Install MDK dependencies
RUN npm install

# Create workspace and set ownership
RUN mkdir -p /workspace && chown -R claude:claude /workspace
RUN chown -R claude:claude /app/template-app-1

# Copy runner script and prompts to tools directory
RUN mkdir -p /tools
COPY run-claude.sh /tools/
COPY prompt-user.md /tools/
COPY prompt-system.md /tools/
RUN chmod +x /tools/run-claude.sh

# Set working directory to workspace for runtime
WORKDIR /workspace

# Switch to non-root user
USER claude

CMD ["/tools/run-claude.sh"]