FROM ruby:3.4.8

# Install system dependencies including Node.js
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y \
    build-essential \
    git \
    libpq-dev \
    libyaml-dev \
    postgresql-client \
    libvips \
    pkg-config \
    curl \
    vim \
    nodejs \
    npm \
    chromium \
    chromium-driver \
    && rm -rf /var/lib/apt/lists/* /var/cache/apt/archives

# Install Bun
RUN curl -fsSL https://bun.sh/install | bash && \
    mv ~/.bun/bin/bun /usr/local/bin/

# Set working directory
WORKDIR /app

# Install npm dependencies for Vite
COPY package.json bun.lock ./
# Local file deps in package.json need to exist before bun install.
COPY vendor/inertia/packages/core/package.json ./vendor/inertia/packages/core/package.json
COPY vendor/inertia/packages/svelte/package.json ./vendor/inertia/packages/svelte/package.json
RUN bun install

# Install application dependencies
COPY Gemfile Gemfile.lock ./
RUN bundle install

# Add a script to be executed every time the container starts
COPY entrypoint.dev.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.dev.sh
ENTRYPOINT ["entrypoint.dev.sh"]

# Global git safeguards
RUN git config --system http.timeout 30 && \
    git config --system http.lowSpeedLimit 1000 && \
    git config --system http.lowSpeedTime 10
EXPOSE 3000
EXPOSE 3036

# Disable dev warnings
RUN bundle exec skylight disable_dev_warning
RUN bundle config set default_cli_command install --global

# Start the main process
CMD ["rails", "server", "-b", "0.0.0.0"]
