name: Build Windows

on:
  workflow_call:
    inputs:
      run-tests:
        type: boolean
        default: false
      run-clippy:
        type: boolean
        default: false
      build-packages:
        type: boolean
        default: false
  workflow_dispatch:
    inputs:
      run-tests:
        type: boolean
        default: true
      run-clippy:
        type: boolean
        default: true
      build-packages:
        type: boolean
        default: true

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: windows-latest
    timeout-minutes: 60

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-pc-windows-msvc
          components: clippy

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
        with:
          key: x86_64-pc-windows-msvc

      - name: Install dependencies
        run: |
          choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System'
          echo "C:\Program Files\CMake\bin" >> $env:GITHUB_PATH

      - name: Build
        run: cargo build --release --target x86_64-pc-windows-msvc

      - name: Build desktop
        run: cargo build --release --features desktop --bin cagire-desktop --target x86_64-pc-windows-msvc

      - name: Test
        if: inputs.run-tests
        run: cargo test --target x86_64-pc-windows-msvc

      - name: Clippy
        if: inputs.run-clippy
        run: cargo clippy --target x86_64-pc-windows-msvc -- -D warnings

      - name: Bundle CLAP plugin
        if: inputs.build-packages
        run: cargo xtask bundle cagire-plugins --release --target x86_64-pc-windows-msvc

      - name: Install NSIS
        if: inputs.build-packages
        run: choco install nsis

      - name: Build NSIS installer
        if: inputs.build-packages
        shell: pwsh
        run: |
          $version = (Select-String -Path Cargo.toml -Pattern '^version\s*=\s*"(.+)"' | Select-Object -First 1).Matches.Groups[1].Value
          $root = (Get-Location).Path
          $target = "x86_64-pc-windows-msvc"
          & "C:\Program Files (x86)\NSIS\makensis.exe" `
            "-DVERSION=$version" `
            "-DCLI_EXE=$root\target\$target\release\cagire.exe" `
            "-DDESKTOP_EXE=$root\target\$target\release\cagire-desktop.exe" `
            "-DICON=$root\assets\Cagire.ico" `
            "-DOUTDIR=$root\target" `
            nsis/cagire.nsi

      - name: Upload CLI artifact
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: cagire-windows-x86_64
          path: target/x86_64-pc-windows-msvc/release/cagire.exe

      - name: Upload desktop artifact
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: cagire-windows-x86_64-desktop
          path: target/x86_64-pc-windows-msvc/release/cagire-desktop.exe

      - name: Upload installer artifact
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: cagire-windows-x86_64-installer
          path: target/cagire-*-setup.exe

      - name: Prepare plugin artifacts
        if: inputs.build-packages
        shell: bash
        run: |
          mkdir -p staging/clap staging/vst3
          cp -R target/bundled/cagire-plugins.clap staging/clap/
          cp -R target/bundled/cagire-plugins.vst3 staging/vst3/

      - name: Upload CLAP artifact
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: cagire-windows-x86_64-clap
          path: staging/clap/

      - name: Upload VST3 artifact
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: cagire-windows-x86_64-vst3
          path: staging/vst3/
