name: Build CCExtractor on Windows

env:
  RUSTFLAGS: -Ctarget-feature=+crt-static
  VCPKG_DEFAULT_TRIPLET: x64-windows-static
  VCPKG_COMMIT: ab2977be50c702126336e5088f4836060733c899

on:
  workflow_dispatch:
  push:
    paths:
      - ".github/workflows/build_windows.yml"
      - "**.c"
      - "**.h"
      - "**CMakeLists.txt"
      - "**.cmake"
      - "windows/**"
      - "src/rust/**"
  pull_request:
    types: [opened, synchronize, reopened]
    paths:
      - ".github/workflows/build_windows.yml"
      - "**.c"
      - "**.h"
      - "**CMakeLists.txt"
      - "**.cmake"
      - "windows/**"
      - "src/rust/**"

jobs:
  build:
    runs-on: windows-2022
    steps:
      - name: Check out repository
        uses: actions/checkout@v6

      - name: Setup MSBuild.exe
        uses: microsoft/setup-msbuild@v2.0.0
        with:
          msbuild-architecture: x64

      # Install GPAC (fast, ~30s, not worth caching complexity)
      - name: Install gpac
        run: choco install gpac --version 2.4.0 --no-progress

      # Use lukka/run-vcpkg for better caching
      - name: Setup vcpkg
        uses: lukka/run-vcpkg@v11
        id: runvcpkg
        with:
          vcpkgGitCommitId: ${{ env.VCPKG_COMMIT }}
          vcpkgDirectory: ${{ github.workspace }}/vcpkg
          vcpkgJsonGlob: 'windows/vcpkg.json'

      # Cache vcpkg installed packages separately for faster restores
      - name: Cache vcpkg installed packages
        id: vcpkg-installed-cache
        uses: actions/cache@v5
        with:
          path: ${{ github.workspace }}/vcpkg/installed
          key: vcpkg-installed-${{ runner.os }}-${{ env.VCPKG_COMMIT }}-${{ hashFiles('windows/vcpkg.json') }}
          restore-keys: |
            vcpkg-installed-${{ runner.os }}-${{ env.VCPKG_COMMIT }}-

      - name: Install vcpkg dependencies
        if: steps.vcpkg-installed-cache.outputs.cache-hit != 'true'
        run: ${{ github.workspace }}/vcpkg/vcpkg.exe install --x-install-root ${{ github.workspace }}/vcpkg/installed/
        working-directory: windows

      # Cache Rust/Cargo artifacts
      - name: Cache Cargo registry
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-registry-

      # Cache Cargo build artifacts - rust.bat sets CARGO_TARGET_DIR to windows/
      # which results in artifacts at windows/x86_64-pc-windows-msvc/
      - name: Cache Cargo build artifacts
        uses: actions/cache@v5
        with:
          path: ${{ github.workspace }}/windows/x86_64-pc-windows-msvc
          key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('src/rust/**/*.rs') }}
          restore-keys: |
            ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}-
            ${{ runner.os }}-cargo-build-

      - name: Setup Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Install Win 10 SDK
        uses: ilammy/msvc-dev-cmd@v1

      # Build Release-Full
      - name: Build Release-Full
        env:
          LIBCLANG_PATH: "C:\\Program Files\\LLVM\\lib"
          LLVM_CONFIG_PATH: "C:\\Program Files\\LLVM\\bin\\llvm-config"
          BINDGEN_EXTRA_CLANG_ARGS: -fmsc-version=0
          VCPKG_ROOT: ${{ github.workspace }}/vcpkg
        run: msbuild ccextractor.sln /p:Configuration=Release-Full /p:Platform=x64
        working-directory: ./windows

      - name: Display Release version information
        run: ./ccextractorwinfull.exe --version
        working-directory: ./windows/x64/Release-Full

      - name: Upload Release artifact
        uses: actions/upload-artifact@v6
        with:
          name: CCExtractor Windows Release build
          path: |
            ./windows/x64/Release-Full/ccextractorwinfull.exe
            ./windows/x64/Release-Full/*.dll

      # Build Debug-Full (reuses cached Cargo artifacts)
      - name: Build Debug-Full
        env:
          LIBCLANG_PATH: "C:\\Program Files\\LLVM\\lib"
          LLVM_CONFIG_PATH: "C:\\Program Files\\LLVM\\bin\\llvm-config"
          BINDGEN_EXTRA_CLANG_ARGS: -fmsc-version=0
          VCPKG_ROOT: ${{ github.workspace }}/vcpkg
        run: msbuild ccextractor.sln /p:Configuration=Debug-Full /p:Platform=x64
        working-directory: ./windows

      - name: Display Debug version information
        continue-on-error: true
        run: ./ccextractorwinfull.exe --version
        working-directory: ./windows/x64/Debug-Full

      - name: Upload Debug artifact
        uses: actions/upload-artifact@v6
        with:
          name: CCExtractor Windows Debug build
          path: |
            ./windows/x64/Debug-Full/ccextractorwinfull.exe
            ./windows/x64/Debug-Full/ccextractorwinfull.pdb
            ./windows/x64/Debug-Full/*.dll
