name: Windows

on:
  push:
  release:
    types: [published]

env:
  PHP_EXT: lz4
  PHP_EXT_VERSION: ${{ github.event.release.tag_name }}
  BIN_SDK_VER: 2.5.0

jobs:
  ci:
    strategy:
      matrix:
        php:
          - '8.4'
          - '8.3'
          - '8.2'
          - '8.1'
        arch:
          - x64
          - x86
        ts:
          - 'nts'
          - 'ts'
        vs:
          - vs16
          - vs17
        library:
          - false
          - true
        exclude:
          - php: '8.4'
            vs: vs16
          - php: '8.3'
            vs: vs17
          - php: '8.2'
            vs: vs17
          - php: '8.1'
            vs: vs17

    runs-on: windows-latest

    env:
      PHP_VER: ${{ matrix.php }}
      VS: ${{ matrix.vs }}
      ARCH: ${{ matrix.arch }}
      TS: ${{ matrix.ts }}

    if: contains(github.event.head_commit.message, 'ci skip') == false

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Checkout submodule
        run: .\.github\workflows\submodule.ps1
        shell: pwsh
        if: ${{ ! matrix.library }}
      - name: Install dependency library
        run: .\.github\workflows\vcpkg.ps1
        shell: pwsh
        env:
          VCPKG_LIBRARY: lz4
        if: ${{ matrix.library }}

      - uses: actions/cache@v4
        with:
          path: |
            C:\php\php-*.zip
          key: ${{ runner.os }}-php-${{ matrix.php }}-${{ matrix.ts }}-${{ matrix.vs }}-${{ matrix.arch }}
          restore-keys: |
            ${{ runner.os }}-php-

      - name: Install build command
        run: .\.github\workflows\install.ps1
        shell: pwsh

      - name: Build
        run: .\.github\workflows\build.ps1
        shell: pwsh

      - name: Test
        run: .\.github\workflows\test.ps1
        shell: pwsh
        env:
          REPORT_EXIT_STATUS: 1
          NO_INTERACTION: 1

      - if: ${{ github.event_name == 'release' && matrix.library == false }}
        name: Archive DLL
        run: |-
          Copy-Item .\php_${{ env.PHP_EXT }}.dll .\$env:EXT_NAME.dll
          Compress-Archive -Path .\$env:EXT_NAME.dll -Destination .\$env:EXT_NAME.zip
        shell: pwsh
        env:
          EXT_NAME: php_${{ env.PHP_EXT }}-${{ env.PHP_EXT_VERSION }}-${{ matrix.php }}-${{ matrix.ts }}-${{ matrix.vs }}-${{ matrix.arch == 'x64' && 'x86_64' || matrix.arch }}

      - if: ${{ github.event_name == 'release' && matrix.library == false }}
        name: Store archive DLL
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.PHP_EXT_VERSION }}-${{ matrix.php }}-${{ matrix.ts }}-${{ matrix.vs }}-${{ matrix.arch == 'x64' && 'x86_64' || matrix.arch }}
          path: .\php_${{ env.PHP_EXT }}-${{ env.PHP_EXT_VERSION }}-${{ matrix.php }}-${{ matrix.ts }}-${{ matrix.vs }}-${{ matrix.arch == 'x64' && 'x86_64' || matrix.arch }}.zip
          overwrite: true

  release:
    permissions:
      contents: write
    needs: ci

    runs-on: ubuntu-latest

    if: ${{ github.event_name == 'release' }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Get artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - name: Upload artifacts
        run: gh release upload ${{ env.PHP_EXT_VERSION }} artifacts/php* --clobber
        shell: bash
        env:
          GH_TOKEN: ${{ github.token }}
