#!/usr/bin/env bash

set -e

# Colors and formatting
GREEN='\033[1;32m'
BLUE='\033[1;34m'
NC='\033[0m' # No Color

# Temporary file for shell environment lines
SHELL_ENV_FILE=$(mktemp)
# delete the file after the script is finished
trap 'rm -f "$SHELL_ENV_FILE"' EXIT

function step() {
  echo -e "\n${BLUE}$1${NC}"
}

function install::ruby_rbenv() {
  if ! command -v rbenv &>/dev/null; then
    step "Installing rbenv for Ruby version management..."
    brew install rbenv || echo ""

    # Add shell configuration
    echo 'export PATH="$(brew --prefix rbenv)/bin:$PATH"' >>"$SHELL_ENV_FILE"
    echo 'eval "$(rbenv init -)"' >>"$SHELL_ENV_FILE"
  else
    step "rbenv is already installed."
  fi

  export PATH="$(brew --prefix rbenv)/bin:$PATH"
  eval "$(rbenv init -)"
}

function install::ruby_version() {
  # Read Ruby version from .ruby-version file
  RUBY_VERSION=$(cat ".ruby-version" | tr -d '[:space:]')
  step "Installing Ruby ${RUBY_VERSION}..."

  # Install Ruby version from .ruby-version if not already installed
  if ! rbenv versions | grep -q "${RUBY_VERSION}"; then
    rbenv install "${RUBY_VERSION}"
    rbenv global "${RUBY_VERSION}"
  else
    step "Ruby ${RUBY_VERSION} is already installed."
  fi
}

function install::ruby_bundler() {
  if ! command -v bundle &>/dev/null; then
    step "Installing Bundler..."
    gem install bundler
  else
    step "Bundler is already installed."
  fi
}

function install::ruby_gems() {
  step "Installing Ruby dependencies from Gemfile..."
  bundle install
}

function install::pre_commit() {
  if ! command -v pre-commit &>/dev/null; then
    step "Installing pre-commit..."
    brew install pre-commit
  else
    step "pre-commit is already installed."
  fi

  step "Setting up pre-commit hooks..."
  pre-commit install
}

function install::shfmt() {
  if ! command -v shfmt &>/dev/null; then
    step "Installing shfmt..."
    brew install shfmt
  else
    step "shfmt is already installed."
  fi
}

function install::gh() {
  if ! command -v gh &>/dev/null; then
    step "Installing gh..."
    brew install gh
  else
    step "gh is already installed."
  fi
  # check if gh is authenticated
  if ! gh auth status &>/dev/null; then
    step "Authenticating gh..."
    gh auth login
  else
    step "gh is already authenticated."
  fi
}

function install::imagemagick() {
  if ! command -v convert &>/dev/null; then
    step "Installing ImageMagick..."
    brew install imagemagick
  else
    step "ImageMagick is already installed."
  fi
}

function main() {
  echo "--------------------------"
  echo "Local Development Setup"
  echo "--------------------------"

  # Development tools setup
  install::pre_commit
  install::shfmt
  install::gh
  install::imagemagick

  # Ruby environment setup
  install::ruby_rbenv
  install::ruby_version
  install::ruby_bundler
  install::ruby_gems

  step "All required dependencies are installed!"

  if [ ! -f "$SHELL_ENV_FILE" ]; then
    echo -e "\n${GREEN}Please add these lines to your shell's RC file (${SHELL:-bash}):"
    echo -e "${GREEN}----------------------------------------${NC}"
    cat "$SHELL_ENV_FILE"
    echo -e "${GREEN}----------------------------------------${NC}"
    echo -e "${GREEN}Then restart your shell or source your RC file${NC}\n"
    rm -f "$SHELL_ENV_FILE"
  fi

  echo "Installation finished successfully!"
}

# Execute main function
main
