name: CI (Windows)

on:
  workflow_call:
    inputs:
      arch:
        required: true
        type: string

env:
  CTEST_OUTPUT_ON_FAILURE: 1

jobs:
  windows:
    name: CI

    runs-on: windows-latest

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

    - name: Setup vcpkg
      id: vcpkg
      uses: OpenTTD/actions/setup-vcpkg@v6

    - name: Install OpenGFX
      shell: bash
      run: |
        mkdir -p "C:/Users/Public/Documents/OpenTTD/baseset"
        cd "C:/Users/Public/Documents/OpenTTD/baseset"

        echo "::group::Download OpenGFX"
        curl -L https://cdn.openttd.org/opengfx-releases/0.6.0/opengfx-0.6.0-all.zip -o opengfx-all.zip
        echo "::endgroup::"

        echo "::group::Unpack OpenGFX"
        unzip opengfx-all.zip
        echo "::endgroup::"

        rm -f opengfx-all.zip

    - name: Install MSVC problem matcher
      uses: ammaraskar/msvc-problem-matcher@master

    - name: Configure developer command prompt for ${{ inputs.arch }}
      uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: ${{ inputs.arch }}

    - name: Build
      shell: bash
      env:
        NINJA_STATUS: "[%f/%t -- %e] " # [finished_edges/total_edges -- elapsed_time], default value is "[%f/%t] "
      run: |
        mkdir build
        cd build

        echo "::group::CMake"
        cmake .. \
          -GNinja \
          -DVCPKG_TARGET_TRIPLET=${{ inputs.arch }}-windows-static \
          -DCMAKE_TOOLCHAIN_FILE="${{ steps.vcpkg.outputs.vcpkg-cmake }}" \
          # EOF
        echo "::endgroup::"

        echo "::group::Build"
        cmake --build .
        echo "::endgroup::"

    - name: Test
      shell: bash
      run: |
        cd build
        ctest --timeout 120
