FROM ruby:2.6.10-slim-bullseye

# Install dependencies (MySQL client + ImageMagick for CarrierWave/MiniMagick)
RUN apt-get update -qq && apt-get install -y --no-install-recommends \
    build-essential \
    default-libmysqlclient-dev \
    nodejs \
    git \
    curl \
    imagemagick \
    libmagickwand-dev \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install correct bundler version (must match Gemfile.lock BUNDLED WITH)
RUN gem install bundler:2.4.21

# Install gems
COPY Gemfile Gemfile.lock ./
RUN bundle install --deployment --without development test --jobs 4

# Copy application
COPY . .

# Create required directories and runtime files
RUN mkdir -p tmp/pids tmp/puma tmp/cache tmp/sockets log storage public/uploads \
    && echo '{"data":[]}' > config/shops.json

# Precompile assets
RUN RAILS_ENV=production SECRET_KEY_BASE=placeholder bundle exec rake assets:precompile 2>/dev/null || true

EXPOSE 3000

COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

CMD ["/app/entrypoint.sh"]
