name: SSH on runner
on:
  workflow_dispatch:
    inputs:
      os:
        description: "Operating System"
        required: true
        default: ubuntu-latest
        type: choice
        options:
          - ubuntu-latest
          - macos-latest
          - windows-latest
      mod:
        description: "Install Go/Node modules"
        required: true
        default: true
        type: boolean

jobs:
  setup-ssh:
    name: Setup runner and open SSH endpoint
    runs-on: ${{ github.event.inputs.os }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Load variables from file
        uses: antifree/json-to-variables@v1.0.1
        with:
          filename: .github/workflows/utils/variables.json

      - name: Setup asdf
        uses: asdf-vm/actions/setup@9cd779f40fe38688dd19505ccbc4eaaf018b44e7
        with:
          asdf_version: 0.16.7

      - name: Setup go
        if: runner.os != 'Windows'
        run: |
          asdf plugin add golang
          asdf install golang
          echo "go_version=$(asdf current golang | xargs | cut -d ' ' -f 6)" >> $GITHUB_ENV

      - name: Setup node
        if: runner.os != 'Windows'
        working-directory: js
        run: |
          asdf plugin add nodejs
          asdf install nodejs
          echo "node_version=$(asdf current nodejs | xargs | cut -d ' ' -f 6)" >> $GITHUB_ENV

      - name: Setup yarn
        if: runner.os != 'Windows'
        working-directory: js
        run: |
          asdf plugin add yarn
          asdf install yarn

      - name: Cache go modules
        if: github.event.inputs.mod == 'true' && runner.os != 'Windows'
        uses: actions/cache@v4
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-${{ hashFiles('go/**/go.sum') }}
          restore-keys: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-

      - name: Cache node modules
        if: github.event.inputs.mod == 'true' && runner.os != 'Windows'
        uses: actions/cache@v4
        with:
          path: js/node_modules
          key: ${{ runner.OS }}-node-${{ env.node_version }}-${{ env.json_cache-versions_node }}-${{ hashFiles('js/yarn.lock') }}
          restore-keys: ${{ runner.OS }}-node-${{ env.node_version }}-${{ env.json_cache-versions_node }}-

      - name: Fetch go modules
        if: github.event.inputs.mod == 'true' && runner.os != 'Windows'
        working-directory: go
        run: go mod tidy

      - name: Fetch node modules
        if: github.event.inputs.mod == 'true' && runner.os != 'Windows'
        working-directory: js
        run: make node_modules

      - name: Install emacs
        shell: bash
        run: |
          if [ "$RUNNER_OS" == "Linux" ]; then
              sudo apt-get install -y emacs
          elif [ "$RUNNER_OS" == "Windows" ]; then
              choco install emacs
          else
              echo "Already installed!"
          fi

      - name: Install LazyVim
        shell: bash
        run: |
          if [ "$RUNNER_OS" == "Windows" ]; then
              git clone https://github.com/LazyVim/starter $env:LOCALAPPDATA\nvim
          else
              git clone https://github.com/LazyVim/starter ~/.config/nvim
          fi

      - name: Setup tmate session
        uses: mxschmitt/action-tmate@v3
        with:
          limit-access-to-actor: true
