name: CI

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

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '8.0.x'

      - name: Detect projects
        id: detect
        run: |
          PROJECTS=$(find . -name '*.csproj' -not -path './.git/*')
          COUNT=$(printf '%s\n' "$PROJECTS" | grep -c . || true)
          echo "count=$COUNT" >> "$GITHUB_OUTPUT"
          if [ "$COUNT" -gt 0 ]; then
            echo "Found projects:" >> "$GITHUB_STEP_SUMMARY"
            printf '%s\n' "$PROJECTS" >> "$GITHUB_STEP_SUMMARY"
          else
            echo "No .csproj files found" >> "$GITHUB_STEP_SUMMARY"
          fi

      - name: Restore
        if: steps.detect.outputs.count != '0'
        run: dotnet restore

      - name: Build
        if: steps.detect.outputs.count != '0'
        run: dotnet build --no-restore --configuration Release

      - name: Test
        if: steps.detect.outputs.count != '0'
        run: dotnet test --no-build --configuration Release --logger trx --results-directory TestResults

      - name: No projects to build
        if: steps.detect.outputs.count == '0'
        run: echo "No .csproj found; nothing to build."
