name: BuildWindows

on: workflow_dispatch

jobs:
  buildAssets:
    uses: ./.github/workflows/build_assets.yml

  buildWin64:
    needs: buildAssets
    runs-on: windows-latest
    steps:
      - name: Python Setup
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          architecture: x64

      - name: Checkout Source
        uses: actions/checkout@v4

      - name: Download Artifacts
        uses: actions/download-artifact@v4
        with:
          name: nw-assets
          path: novelwriter/assets

      - name: Build Setup Installer
        id: build
        run: |
          echo "BUILD_VERSION=$(python pkgutils.py version)" >> $Env:GITHUB_OUTPUT
          python pkgutils.py build-win-exe

      - name: Upload Unsigned Artifacts
        id: upload-unsigned-artifact
        uses: actions/upload-artifact@v4
        with:
          name: novelWriter-${{ steps.build.outputs.BUILD_VERSION }}-Win-Setup
          path: dist/*.exe
          if-no-files-found: error
          retention-days: 14

      - name: Submit Signing Request
        uses: signpath/github-action-submit-signing-request@v1
        with:
          api-token: "${{ secrets.SIGNPATH_API_TOKEN }}"
          organization-id: "0471e52c-66fa-4e9a-bfb9-36167095ca3f"
          project-slug: "novelWriter"
          signing-policy-slug: "release-signing "
          github-artifact-id: "${{ steps.upload-unsigned-artifact.outputs.artifact-id }}"
          wait-for-completion: true
          output-artifact-directory: "dist/"
          parameters: |
            version: "${{ steps.build.outputs.BUILD_VERSION }}"

      - name: Upload Signed Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: novelWriter-${{ steps.build.outputs.BUILD_VERSION }}-Win-Setup-Signed
          path: dist/*.exe
          if-no-files-found: error
          retention-days: 14
