name: 'Setup Go + Webchat'
description: 'Install Go, pnpm, Node.js, build webchat SPA, download Go modules'

inputs:
  go-version:
    description: 'Go version'
    required: false
    default: '1.26'
  node-version:
    description: 'Node.js version'
    required: false
    default: '22'
  pnpm-version:
    description: 'pnpm version'
    required: false
    default: '10'
  build-webchat:
    description: 'Whether to build webchat SPA (if false, still restores from cache)'
    required: false
    default: 'true'
  download-modules:
    description: 'Whether to download Go modules'
    required: false
    default: 'true'

runs:
  using: 'composite'
  steps:
    - uses: actions/setup-go@v6
      with:
        go-version: ${{ inputs.go-version }}
        cache: true

    # Always restore webchat from cache — needed for go vet ./...
    # which compiles internal/webchat even if we don't test it.
    - name: Restore webchat build output
      id: webchat-cache
      uses: actions/cache@v5
      with:
        path: internal/webchat/out
        key: webchat-${{ hashFiles('webchat/src/**', 'webchat/next.config.*', 'webchat/package.json', 'webchat/pnpm-lock.yaml') }}

    - uses: pnpm/action-setup@v4
      if: steps.webchat-cache.outputs.cache-hit != 'true'
      with:
        version: ${{ inputs.pnpm-version }}

    - uses: actions/setup-node@v6
      if: steps.webchat-cache.outputs.cache-hit != 'true'
      with:
        node-version: ${{ inputs.node-version }}
        cache: 'pnpm'
        cache-dependency-path: webchat/pnpm-lock.yaml

    - name: Build webchat SPA
      if: steps.webchat-cache.outputs.cache-hit != 'true'
      shell: bash
      run: make webchat-embed

    - name: Download Go modules
      if: inputs.download-modules == 'true'
      shell: bash
      run: go mod download

    - name: Restore docs build output
      id: docs-cache
      uses: actions/cache@v5
      with:
        path: internal/docs/out
        key: docs-${{ hashFiles('docs/**/*.md', 'cmd/build-docs/**') }}

    - name: Build docs
      if: steps.docs-cache.outputs.cache-hit != 'true'
      shell: bash
      run: go run ./cmd/build-docs
