name: PR Check

on:
  pull_request:
    branches: [main, develop]

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.24'

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Cache Go modules
        uses: actions/cache@v4
        with:
          path: |
            /root/go/pkg/mod
            /home/runner/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      - name: Cache Node modules
        uses: actions/cache@v4
        with:
          path: web/node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('web/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Download dependencies
        run: go mod download

      - name: Build frontend
        run: |
          cd web
          npm ci
          npm run build

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

      - name: Test
        run: go test ./... -v -race -timeout 60s

      - name: Build
        run: |
          go build -o muyue ./cmd/muyue/
          ./muyue version
