name: Windows x64 Build
on: [push, pull_request]

jobs:
  build:
    runs-on: windows-latest
    permissions:
      actions: write
    defaults:
      run:
        shell: msys2 {0}
    steps:
      - uses: actions/checkout@v4

      - uses: msys2/setup-msys2@v2
        with:
          msystem: UCRT64
          update: true
          install: mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-gdb mingw-w64-ucrt-x86_64-allegro mingw-w64-ucrt-x86_64-enet mingw-w64-ucrt-x86_64-fluidsynth mingw-w64-ucrt-x86_64-libmad

      - name: Build
        run: ./scripts/build-windows.sh

      - name: Bundle libs
        run: ./scripts/bundle-libs-ucrt64.sh

      - name: Delete Old Artifacts
        uses: actions/github-script@v7
        id: artifact
        with:
          script: |
            const res = await github.rest.actions.listArtifactsForRepo({
                owner: context.repo.owner,
                repo: context.repo.repo,
            })

            res.data.artifacts
                .filter(({ name }) => name === 'windows-x64-build')
                .forEach(({ id }) => {
                  github.rest.actions.deleteArtifact({
                      owner: context.repo.owner,
                      repo: context.repo.repo,
                      artifact_id: id,
                  })
                })
                
      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: windows-x64-build
          path: dist
