name: PR Checks

on:
  pull_request:
    branches:
      - main
      - release/*

env:
  GOPROXY: https://proxy.golang.org,direct
  GO_VERSION: "1.25"
  NODE_VERSION: "22"

jobs:
  # Quick checks for PRs
  checks:
    name: Code Quality Checks
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Check for existing Go
        id: go-check
        run: |
          if command -v go &> /dev/null; then
            GO_VER=$(go version | grep -oP 'go\d+\.\d+' | head -1)
            echo "version=$GO_VER" >> $GITHUB_OUTPUT
            echo "Found Go: $(go version)"
          fi

      - name: Setup Go
        if: steps.go-check.outputs.version == ''
        uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}
          cache: true

      - name: Check for existing Node.js
        id: node-check
        run: |
          if command -v node &> /dev/null; then
            NODE_VER=$(node --version)
            echo "version=$NODE_VER" >> $GITHUB_OUTPUT
            echo "Found Node.js: $NODE_VER"
          fi

      - name: Setup Node.js
        if: steps.node-check.outputs.version == ''
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Check for existing pnpm
        id: pnpm-check
        run: |
          if command -v pnpm &> /dev/null; then
            PNPM_VER=$(pnpm --version)
            echo "version=$PNPM_VER" >> $GITHUB_OUTPUT
            echo "Found pnpm: $PNPM_VER"
          fi

      - name: Install pnpm
        if: steps.pnpm-check.outputs.version == ''
        run: npm install -g pnpm

      - name: Install Go dependencies
        run: go mod download

      - name: Check Go formatting
        run: |
          if [ -n "$(gofmt -l .)" ]; then
            echo "Go code is not formatted. Please run 'gofmt -w .'"
            gofmt -l .
            exit 1
          fi

      - name: Go vet
        run: go vet ./...

      - name: Check for build errors
        run: go build -v ./...

  # Unit tests
  unit-tests:
    name: Unit Tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Check for existing Go
        id: go-check
        run: |
          if command -v go &> /dev/null; then
            GO_VER=$(go version | grep -oP 'go\d+\.\d+' | head -1)
            echo "version=$GO_VER" >> $GITHUB_OUTPUT
            echo "Found Go: $(go version)"
          fi

      - name: Setup Go
        if: steps.go-check.outputs.version == ''
        uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}
          cache: true

      - name: Run unit tests
        run: |
          go test -v -race -coverprofile=coverage.out ./...
        continue-on-error: true

      - name: Upload coverage
        uses: actions/upload-artifact@v4
        with:
          name: coverage
          path: coverage.out
          retention-days: 7

  # Frontend checks
  frontend:
    name: Frontend Checks
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Check for existing Node.js
        id: node-check
        run: |
          if command -v node &> /dev/null; then
            NODE_VER=$(node --version)
            echo "version=$NODE_VER" >> $GITHUB_OUTPUT
            echo "Found Node.js: $NODE_VER"
          fi

      - name: Setup Node.js
        if: steps.node-check.outputs.version == ''
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Check for existing pnpm
        id: pnpm-check
        run: |
          if command -v pnpm &> /dev/null; then
            PNPM_VER=$(pnpm --version)
            echo "version=$PNPM_VER" >> $GITHUB_OUTPUT
            echo "Found pnpm: $PNPM_VER"
          fi

      - name: Install pnpm
        if: steps.pnpm-check.outputs.version == ''
        run: npm install -g pnpm

      - name: Install dependencies
        run: pnpm install --frozen-lockfile
        continue-on-error: true

      - name: TypeScript check
        run: pnpm run tsc
        continue-on-error: true

      - name: ESLint
        run: pnpm run eslint
        continue-on-error: true
