name: CI/CD Pipeline

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

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}
  GO_VERSION: '1.24'
  NODE_VERSION: '20'

jobs:
  test-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: 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 formatting
      run: make fmt-check
    
    - name: Build binary
      run: make build

  test-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 web lint
      run: make web-lint
    
    - name: Run web tests
      run: make web-test
    
    - name: Build web assets
      run: make web-build
