#!/usr/bin/env bash
# shellcheck shell=bash

set -e

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

# Temporary file for shell environment lines
if [ -z "${SHELL_ENV_FILE:-}" ]; then
  SHELL_ENV_FILE=$(mktemp)
  # delete the file after the script is finished
  trap 'rm -f "$SHELL_ENV_FILE"' EXIT
  SHELL_ENV_PRINT=true
fi

function ok() {
  echo -e "${BOLD}${GREEN}[OK]${NC} ${BLUE}$1${NC} ${BOLD}${2:-}${NC}"
}

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

    # shellcheck disable=SC2016
    echo 'export PATH="$(brew --prefix rbenv)/bin:$PATH"' >>"$SHELL_ENV_FILE"
    # shellcheck disable=SC2016
    echo 'eval "$(rbenv init -)"' >>"$SHELL_ENV_FILE"
  fi

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

  ok "rbenv" "$(rbenv --version | cut -d' ' -f2)"
}

function install::ruby() {
  # Read Ruby version from .ruby-version file
  RUBY_VERSION=$(cat "./.ruby-version" | tr -d '[:space:]')
  # Install Ruby version from .ruby-version if not already installed
  if ! rbenv versions | grep -q "${RUBY_VERSION}"; then
    echo "Installing Ruby ${RUBY_VERSION}..."
    rbenv install "${RUBY_VERSION}"
  fi

  ok "Ruby" "${RUBY_VERSION}"
}

function install::bundler() {
  if ! command -v bundle &>/dev/null; then
    echo "Installing Bundler..."
    gem install bundler
  fi

  ok "Ruby bundler" "$(bundle -v | cut -d' ' -f3)"
}

function install::bundle_dependencies() {
  # check if all gems are installed
  if ! bundle check &>/dev/null; then
    echo "Installing Ruby gems..."
    bundle install
  fi

  ok "Ruby gems"
}

function install::shfmt() {
  if ! command -v shfmt &>/dev/null; then
    echo "Installing shfmt..."
    brew install shfmt
  fi

  ok "shfmt" "$(shfmt --version | cut -d' ' -f2)"
}

function install::xcode() {
  if ! xcode-select -p &>/dev/null; then
    echo "Installing Xcode Command Line Tools..."
    xcode-select --install
    echo "Please restart the terminal after installation completes"
  fi

  # get Xcode version from .xcode-version file
  XCODE_VERSION="$(cat ".xcode-version" | tr -d '[:space:]')"
  current_version="$(xcodebuild -version | head -n 1 | cut -d ' ' -f 2)"

  # normalize versions to major.minor
  XCODE_VERSION_SHORT="$(echo "$XCODE_VERSION" | awk -F. '{print $1"."$2}')"
  current_version_SHORT="$(echo "$current_version" | awk -F. '{print $1"."$2}')"

  # Select the Xcode version and fail if not found
  if [[ "${XCODE_VERSION_SHORT}" != "${current_version_SHORT}" ]]; then
    echo "${RED}Xcode ${XCODE_VERSION} is not installed. Please install it from the App Store.${NC}"
    exit 1
  fi

  ok "Xcode" "${XCODE_VERSION}"
}

function install::xcbeautify() {
  if ! command -v xcbeautify &>/dev/null; then
    echo "Installing xcbeautify..."
    brew install xcbeautify
  fi
  ok "xcbeautify" "$(xcbeautify --version | cut -d' ' -f2)"
}

function install::swiftgen() {
  ok "swiftgen" "$(./scripts/swiftgen --version | awk -F' ' '{print $2}')"
}

function install::git_hooks() {
  ./scripts/install-git-hooks.sh
}

function configure::xcode() {
  ./scripts/configure_xcode.sh
}

function main() {
  echo -e "${GREEN}------------------------------------${NC}"
  echo -e "${GREEN}iOS Development Setup               ${NC}"
  echo -e "${GREEN}------------------------------------${NC}"

  # Ruby environment setup
  install::rbenv
  install::ruby
  install::bundler
  install::bundle_dependencies

  # iOS development tools
  install::xcode
  install::shfmt
  install::xcbeautify
  install::swiftgen
  install::git_hooks

  # Configuration
  configure::xcode

  if [[ -n "$(git remote get-url origin | grep 'ios')" ]]; then
    echo -e "\n${GREEN}✅ Setup Completed! ${NC}"

    if [ -f "$SHELL_ENV_FILE" ] && [ -s "$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
  fi
}

# Execute main function
main
