# Use a minimal base image
FROM debian:bookworm AS base

# Install dependencies for Go, Node.js, SQLite3, and other required tools
RUN apt-get update && apt-get install -y \
    curl \
    build-essential \
    ca-certificates \
    git \
    wget \
    sqlite3

# Install Go 1.23 manually
RUN wget https://go.dev/dl/go1.23.0.linux-amd64.tar.gz && \
    tar -C /usr/local -xzf go1.23.0.linux-amd64.tar.gz && \
    rm go1.23.0.linux-amd64.tar.gz

# Set Go paths
ENV PATH="/usr/local/go/bin:${PATH}"

# Install Node.js and npm (using NodeSource for Node.js 18.x, which includes npm)
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
    apt-get install -y nodejs

# Verify that Node.js and npm were installed successfully
RUN node -v && npm -v

# Install Tailwind CSS globally
RUN npm install -g tailwindcss

# Install Air for live reload (optional)
RUN /usr/local/go/bin/go install github.com/air-verse/air@latest

# Set the working directory
WORKDIR /app
COPY . .

# Create a stage for Claude Code runner
FROM base AS runner
WORKDIR /app

# Create claude user with UID 1000
RUN useradd -m -u 1000 -s /bin/bash claude

# Download and install Claude Code
RUN npm install -g @anthropic-ai/claude-code

ENV PATH="/usr/local/go/bin:/home/claude/go/bin:${PATH}"

COPY ./bin/runner/runner.sh /app/bin/runner/runner.sh
RUN chmod +x /app/bin/runner/runner.sh

# Switch to claude user
USER claude

CMD ["/app/bin/runner/runner.sh"]

# Create a new stage for development
FROM base AS dev
WORKDIR /app
ENV PATH="/usr/local/go/bin:/root/go/bin:${PATH}"

# Command to run in dev mode
CMD ["sh", "-c", "./bin/build/app_dev.sh && ./bin/init/app_dev.sh && ./bin/run/app_web_dev.sh"]

# Create a new stage for production
FROM base AS prod
WORKDIR /app
ENV PATH="/usr/local/go/bin:/root/go/bin:${PATH}"

# Command to run in production mode
CMD ["sh", "-c", "./bin/build/app_prod.sh && ./bin/init/app_prod.sh && ./bin/run/app_web_prod.sh"]
