name: Windows MSVC Build
on:
  workflow_dispatch:
    inputs:
      build_type:
        description: Build type (Release/Debug)
        type: string
        default: Release
      publish_release:
        description: Publish artifacts to GitHub Release
        type: boolean
        default: false
      prerelease:
        description: Mark GitHub Release as prerelease
        type: boolean
        default: true
      version:
        description: Release version/tag (empty = short commit id)
        type: string
        default: ""
  workflow_call:
    inputs:
      build_type:
        description: Build type (Release/Debug)
        type: string
        default: Release
      publish_release:
        description: Publish artifacts to GitHub Release
        type: boolean
        default: false
      prerelease:
        description: Mark GitHub Release as prerelease
        type: boolean
        default: true
      version:
        description: Release version/tag (empty = short commit id)
        type: string
        default: ""
permissions:
  contents: write
jobs:
  build-msvc:
    runs-on: windows-2022
    env:
      BUILD_TYPE: ${{ inputs.build_type || 'Release' }}
      QT_CMAKE_PREFIX: third_party/qt/6.10.1/msvc2022_64/lib/cmake
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          submodules: true
      - name: Setup MSVC
        uses: ilammy/msvc-dev-cmd@v1
        with:
          arch: amd64
          vsversion: 2022
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.x"
      - name: Install Python deps
        shell: pwsh
        run: |
          python -m pip install --upgrade pip
          pip install aqtinstall
      - name: Install Qt
        shell: pwsh
        run: |
          python scripts/install_qt.py
      - name: Install vcpkg
        shell: pwsh
        run: |
          python scripts/install_vcpkg.py
      - name: Configure
        shell: pwsh
        run: |
          $toolchain = (Resolve-Path ".\third_party\vcpkg\scripts\buildsystems\vcpkg.cmake").Path
          if (-not (Test-Path $toolchain)) {
            Write-Host "Toolchain file not found at $toolchain"
            exit 1
          }
          cmake -S . -B build -G "Visual Studio 17 2022" -A x64 -DCMAKE_INSTALL_PREFIX=install -DENABLE_LTO=ON -DCMAKE_PREFIX_PATH="$env:QT_CMAKE_PREFIX" -DCMAKE_TOOLCHAIN_FILE="$toolchain"
      - name: Build
        shell: pwsh
        run: |
          cmake --build build --config $env:BUILD_TYPE -j
      - name: Install
        shell: pwsh
        run: |
          cmake --install build --config $env:BUILD_TYPE
      - name: Install portable component
        shell: pwsh
        run: |
          cmake --install build --component portable --config $env:BUILD_TYPE --prefix install-portable
      - name: Upload artifacts
        uses: actions/upload-artifact@v6
        with:
          name: Windows-MSVC-${{ github.sha }}-${{ inputs.build_type || 'Release' }}
          path: |
            install/**
            install-portable/**
      - name: Prepare release metadata
        id: release_meta
        if: ${{ inputs.publish_release }}
        shell: pwsh
        run: |
          $shortSha = $env:GITHUB_SHA.Substring(0, 7)
          "short_sha=$shortSha" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
          $version = "${{ inputs.version }}"
          if ([string]::IsNullOrWhiteSpace($version)) { $version = $shortSha }
          "version=$version" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
          $msg = (git log -1 --pretty=%B)
          "message<<EOF" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
          $msg | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
          "EOF" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
      - name: Package artifacts for release
        if: ${{ inputs.publish_release }}
        shell: pwsh
        run: |
          $version = "${{ steps.release_meta.outputs.version }}"
          $buildType = "${{ inputs.build_type || 'Release' }}"
          $zip1 = "PrismLauncher-Windows-MSVC-$version-$buildType.zip"
          $zip2 = "PrismLauncher-Windows-MSVC-Portable-$version-$buildType.zip"
          if (Test-Path $zip1) { Remove-Item -Force $zip1 }
          if (Test-Path $zip2) { Remove-Item -Force $zip2 }
          Compress-Archive -Path "install\*" -DestinationPath $zip1 -Force
          Compress-Archive -Path "install-portable\*" -DestinationPath $zip2 -Force
      - name: Publish GitHub Release
        if: ${{ inputs.publish_release }}
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.release_meta.outputs.version }}
          target_commitish: ${{ github.sha }}
          name: Prism Launcher ${{ steps.release_meta.outputs.version }}
          body: ${{ steps.release_meta.outputs.message }}
          prerelease: ${{ inputs.prerelease }}
          files: |
            PrismLauncher-Windows-MSVC-${{ steps.release_meta.outputs.version }}-${{ inputs.build_type || 'Release' }}.zip
            PrismLauncher-Windows-MSVC-Portable-${{ steps.release_meta.outputs.version }}-${{ inputs.build_type || 'Release' }}.zip
