name: PR Validation

on:
  pull_request:
    branches: [ "main" ]
  workflow_dispatch:

env:
  GO_VERSION: '1.24'
  NODE_VERSION: '20'

jobs:
  validate-go:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Go
      uses: actions/setup-go@v5
      with:
        go-version: ${{ env.GO_VERSION }}
        cache: true
    
    - name: Install Protocol Buffers Compiler
      run: |
        sudo apt-get update
        sudo apt-get install -y protobuf-compiler

    - name: Install protoc-gen-go
      run: |
        go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
        echo "$(go env GOPATH)/bin" >> $GITHUB_PATH

    - name: Generate Protocol Buffers
      run: make gen-proto
    
    - name: Install Go dependencies
      run: go mod download
    
    - name: Run Go tests
      run: make test
    
    - name: Check go fmt
      run: make fmt-check
    
    - name: Build binary
      run: make build

  validate-web:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ env.NODE_VERSION }}
        cache: 'npm'
        cache-dependency-path: 'web/pnpm-lock.yaml'
    
    - name: Install pnpm
      uses: pnpm/action-setup@v3
      with:
        version: latest
        run_install: false
    
    - name: Install web dependencies
      working-directory: ./web
      run: pnpm install
    
    - name: Run ESLint
      run: make web-lint
    
    - name: Run web tests
      run: make web-test
    
    - name: Build web assets
      run: make web-build
      
    - name: Upload web test results
      uses: actions/upload-artifact@v4
      if: always()
      with:
        name: web-test-results
        path: web/coverage
  
  check-docker:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    
    - name: Build Docker image
      run: make docker-build
      env:
        MESHSTREAM_SITE_TITLE: "Meshstream Test"
        MESHSTREAM_SITE_DESCRIPTION: "Meshtastic Network Monitoring Test"
    
    - name: Check Docker image
      run: docker images meshstream