name: Workflow

on:
  push:
    branches:
      - "main"
      - "ci-*"
    tags:
      - "v*"
  pull_request:

jobs:
  ci:
    strategy:
      matrix:
        platform:
          - { name: "Linux", arch: "x64", os: ubuntu-latest, werror: true }
          - { name: "Linux", arch: "arm64", os: ubuntu-24.04-arm, werror: true }
          - {
              name: "MacOS",
              arch: "arm64-x64",
              os: macos-latest,
              werror: true,
              cmake-args: '-DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"',
            }
          - {
              name: "Windows",
              arch: "x86",
              os: windows-latest,
              msvc-arch: "Win32",
            }
          - {
              name: "Windows",
              arch: "x64",
              os: windows-latest,
              msvc-arch: "x64",
            }
    #          - { name: 'Windows',  arch: 'arm64',        os: windows-latest, msvc-arch: 'amd64_arm64', cross: true }

    defaults:
      run:
        shell: sh
    runs-on: "${{ matrix.platform.os }}"
    name: "CI ${{ matrix.platform.name }} ${{ matrix.platform.arch }}"
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Set up Ninja
        uses: aseprite/get-ninja@main
      - uses: ilammy/msvc-dev-cmd@v1.13.0
        if: ${{ !!matrix.platform.msvc-arch }}
        with:
          arch: ${{ matrix.platform.msvc-arch }}
      - name: Install Linux dependencies
        if: ${{ runner.os == 'Linux' }}
        run: |
          sudo apt-get update
          sudo apt-get install -y libgl-dev libgl1-mesa-dev libglu1-mesa-dev libxext-dev
      - name: Set up SDL
        uses: libsdl-org/setup-sdl@main
        with:
          add-to-environment: true
          build-type: Release
          install-linux-dependencies: true
          cmake-arguments: ${{ matrix.platform.cmake-args }}
          cmake-generator: Ninja
          cmake-toolchain-file: ${{ matrix.platform.cmake-toolchain-file }}
          discriminator: ${{ matrix.platform.arch }}
          version: 3-latest
          version-sdl2-compat: 2-head
          version-sdl12-compat: 1-head
      - name: "Prepare sources for release"
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        run: |
          echo "${{ github.ref_name }}" >VERSION
      - name: "Configure (CMake)"
        id: configure
        run: |
          cmake -B build -GNinja \
            -DCMAKE_BUILD_TYPE=RelWithDebInfo \
            -DBUILD_TESTS=ON \
            -DDETHRACE_WERROR=${{ !!matrix.platform.werror }} \
            -DCMAKE_POLICY_DEFAULT_CMP0074=NEW \
            -DDETHRACE_INSTALL=ON \
            -DDETHRACE_PACKAGE_PLATFORM=${{ matrix.platform.name }} \
            -DDETHRACE_PACKAGE_ARCH=${{ matrix.platform.arch }} \
            -DCMAKE_TOOLCHAIN_FILE=${{ matrix.platform.cmake-toolchain-file }} \
            -DDETHRACE_PLATFORM_SDL1=ON \
            -DDETHRACE_PLATFORM_SDL2=ON \
            -DDETHRACE_PLATFORM_SDL3=ON \
            -DDETHRACE_PLATFORM_SDL_DYNAMIC=ON \
            ${{ matrix.platform.cmake-args }}
      - name: "Build (CMake)"
        run: |
          cmake --build build
      - name: "Test (CTest)"
        if: ${{ !matrix.platform.cross }}
        run: |
          ctest --test-dir build --verbose
      - name: "Package (CPack)"
        run: |
          cd build
          cpack .
      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: "${{ steps.configure.outputs.filename }}"
          path: "build/dist/${{ steps.configure.outputs.filename }}"
          if-no-files-found: error

  create-release:
    name: Create Release
    needs: [ci]
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
    steps:
      - name: Fetch Build Artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
      - name: Calculate Variables
        id: vars
        run: |
          echo "ref_name_without_v=$(echo ${GITHUB_REF_NAME} | cut -c2-)" >>$GITHUB_OUTPUT
      - name: Create Release
        id: create_release
        uses: softprops/action-gh-release@v2
        with:
          draft: false
          prerelease: false
          name: Dethrace ${{ steps.vars.outputs.ref_name_without_v }}
          generate_release_notes: true
          files: |
            artifacts/**

  reccmp:
    name: Verify decomp
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        name: Checkout
        with:
          submodules: recursive
      - name: Fetch latest report
        run: |
          curl -Lo reccmp-report-report.json https://raw.githubusercontent.com/dethrace-labs/reccmp-report/refs/heads/main/report.json
      - uses: dethrace-labs/reccmp-action@main
        name: Build
        with:
          cmake_flags: -G Ninja -DCMAKE_BUILD_TYPE=Debug -DMSVC_42_FOR_RECCMP=on
          build_only: true
      - uses: dethrace-labs/reccmp-action@main
        name: Run reccmp
        with:
          cmake_flags: -G Ninja -DCMAKE_BUILD_TYPE=Debug -DMSVC_42_FOR_RECCMP=on
          target: CARM95
          diff_report_filename: ${{ github.workspace}}/reccmp-report-report.json
          report_filename: ${{ github.workspace}}/new-reccmp-report.json
          original_binary_url: https://archive.org/download/carm-95/CARM95.EXE
          original_binary_filename: CARM95.EXE
          reccmp_output_filename: ${{ github.workspace}}/reccmp-output.txt
          status_png_icon_filename: ${{ github.workspace}}/reccmp/carma_32x32_4.png
          status_svg_filename: ${{ github.workspace}}/new-reccmp-progress.svg
      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: reccmp-output.txt
          path: ${{ github.workspace}}/reccmp-output.txt
          if-no-files-found: error
      - name: Validate correctness
        run: |
          if grep -q "Decreased" ${{ github.workspace}}/reccmp-output.txt; then
            echo "::warning file=reccmp-output.txt::Decomp correctness decreased"
          fi
      - name: Checkout reccmp-report repo
        if: github.event_name == 'push' && github.ref == 'refs/heads/main' && !github.event.repository.fork
        uses: actions/checkout@v4
        with:
          repository: dethrace-labs/reccmp-report
          token: ${{ secrets.RECCMP_REPORT_TOKEN }}
          path: reccmp-report
      - name: Update report in reccmp-report repo
        if: github.event_name == 'push' && github.ref == 'refs/heads/main' && !github.event.repository.fork
        run: |
          cp new-reccmp-report.json reccmp-report/report.json
          cp new-reccmp-progress.svg reccmp-report/progress.svg
          cd reccmp-report
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git commit -am "Update report from ${GITHUB_SHA::7}" || echo "No changes to commit"
          git push
