# FROM docker.io/cloudflare/sandbox:0.1.3
FROM docker.io/cloudflare/sandbox:0.5.6

ARG TARGETARCH
RUN apt-get update && \
        apt-get install -y git ca-certificates curl procps net-tools && \
        update-ca-certificates && \
        case ${TARGETARCH} in \
            "amd64") ARCH="amd64" ;; \
            "arm64") ARCH="arm64" ;; \
            *) echo "Unsupported architecture: ${TARGETARCH}"; exit 1 ;; \
        esac && \
        curl -L -k --output cloudflared "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-${ARCH}" && \
        chmod +x cloudflared && \
        mv cloudflared /usr/local/bin/ && \
        rm -rf /var/lib/apt/lists/* && \    
        git config --global user.email "vibesdk-bot@cloudflare.com" && \
        git config --global user.name "Cloudflare Vibesdk bot"

# Create directory for error monitoring system
RUN mkdir -p /workspace/container /workspace/data

# Copy the process monitoring system
COPY container/ /workspace/container/

# Expose the ports you want to expose

# Install dependencies for the monitoring system
WORKDIR /workspace/container
RUN bun install && bun run build

# Make scripts executable
RUN chmod +x /workspace/container/cli-tools.ts

# Ensure bunx is available (should be included with bun)
RUN which bunx || ln -sf $(which bun) /usr/local/bin/bunx

# Install tsc via bunx
RUN bunx tsc --version

# Create symlinks for easier CLI usage
RUN ln -sf /workspace/container/cli-tools.ts /usr/local/bin/monitor-cli

# # Setup common templates packages cache
# WORKDIR /app/container/packages-cache
# ENV BUN_INSTALL_CACHE_DIR=/app/container/packages-cache
# RUN bun install

# Set proper permissions for data directory
RUN chmod 755 /workspace/data

# Set environment variable to indicate Docker container environment
ENV CONTAINER_ENV=docker
ENV VITE_LOGGER_TYPE=json 
# # Set environment variable to indicate Docker container environment
# ENV CONTAINER_ENV=docker

# # Reset workdir
WORKDIR /container-server

EXPOSE 3000

# Use startup script
CMD ["./startup.sh"]