name: Validate C++

on:
  pull_request:
  push:
    branches:
      - main
      - 'release-*'
  workflow_dispatch:

env:
  GTEST_COLOR: 1

jobs:
  test:
    name: Build and Test [${{ matrix.toolchain }}][${{ matrix.mode }}]
    runs-on: ${{ (matrix.toolchain == 'MSVC') && 'windows-latest' || 'ubuntu-latest'  }}
    strategy:
      matrix:
        mode: [Debug, Release]
        toolchain: [Clang, GCC] # TODO: fix issues building GTest Binary with MSVC in GitHub Actions

    steps:
      - uses: actions/checkout@v3

      - name: Setup
        uses: ./.github/actions/setup-cpp
        with:
          toolchain: ${{ matrix.toolchain }}

      - name: Unit tests
        run: ./unit_tests ${{ matrix.mode }}


  build_fuzzers:
    name: Build fuzzers [${{ matrix.toolchain }}][${{ matrix.mode }}]
    runs-on: ubuntu-latest
    strategy:
      matrix:
        mode: [Debug, Release]
        toolchain: [Clang]

    steps:
      - uses: actions/checkout@v3

      - name: Setup
        uses: ./.github/actions/setup-cpp
        with:
          toolchain: ${{ matrix.toolchain }}

      - name: Unit tests
        run: ./build_fuzz_tests ${{ matrix.mode }}

  benchmark:
    name: Benchmark [${{ matrix.toolchain }}]
    runs-on: ${{ (matrix.toolchain == 'MSVC') && 'windows-latest' || 'ubuntu-latest'  }}
    strategy:
      matrix:
        toolchain: [Clang, GCC, MSVC]

    steps:
      - uses: actions/checkout@v3

      - name: Setup
        uses: ./.github/actions/setup-cpp
        with:
          toolchain: ${{ matrix.toolchain }}

      - name: Build and run benchmark
        run: |
          cmake -S . -B build -G Ninja -D CMAKE_BUILD_TYPE=Release
          cmake --build build
          ./build/benchmark ./captures
        working-directory: benchmark

      - name: Build and run benchmarklegacy
        run: ./build/benchmarklegacy
        working-directory: benchmark

  capture:
    name: Capture [${{ matrix.toolchain }}]
    runs-on: ${{ (matrix.toolchain == 'MSVC') && 'windows-latest' || 'ubuntu-latest'  }}
    strategy:
      matrix:
        toolchain: [Clang, GCC, MSVC]

    steps:
      - uses: actions/checkout@v3

      - name: Setup
        uses: ./.github/actions/setup-cpp
        with:
          toolchain: ${{ matrix.toolchain }}

      - name: Build capture
        run: |
          cmake -S . -B build -G Ninja -D CMAKE_BUILD_TYPE=Release
          cmake --build build
        working-directory: capture

  clang-format:
    name: Format
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: clang-format
        uses: ./.github/actions/clang-format
