name: Test Windows x64

on:
  pull_request:
    branches:
      - develop
    paths:
      - 'desktop/**'

concurrency:
    group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
    cancel-in-progress: true

jobs:
  test-windows-x64:
    runs-on: windows-latest
    permissions:
      contents: read
    defaults:
      run:
        working-directory: desktop
    env:
      VCPKG_ROOT: ${{ github.workspace }}/vcpkg
    steps:
      - uses: actions/checkout@v6

      - name: Cache vcpkg
        uses: actions/cache@v5
        with:
          path: ${{ env.VCPKG_ROOT }}
          key: ${{ runner.os }}-vcpkg-${{ hashFiles('desktop/**/vcpkg.json') }}
          restore-keys: |
            ${{ runner.os }}-vcpkg-

      - name: Bootstrap vcpkg
        working-directory: ${{ github.workspace }}
        run: |
          if (-not (Test-Path ${{ env.VCPKG_ROOT }})) {
            git clone https://github.com/microsoft/vcpkg.git ${{ env.VCPKG_ROOT }}
          }
          ${{ env.VCPKG_ROOT }}/bootstrap-vcpkg.bat
        shell: pwsh

      - name: Install OpenSSL using vcpkg
        working-directory: ${{ github.workspace }}
        run: ${{ env.VCPKG_ROOT }}/vcpkg install openssl:x64-windows-static --vcpkg-root ${{ env.VCPKG_ROOT }}
        env:
          VCPKG_DEFAULT_TRIPLET: x64-windows-static

      - name: Set OpenSSL env
        shell: bash
        run: |
          echo "OPENSSL_DIR=${{ env.VCPKG_ROOT }}/installed/x64-windows-static" >> $GITHUB_ENV
          echo "OPENSSL_INCLUDE_DIR=${{ env.VCPKG_ROOT }}/installed/x64-windows-static/include" >> $GITHUB_ENV
          echo "OPENSSL_LIB_DIR=${{ env.VCPKG_ROOT }}/installed/x64-windows-static/lib" >> $GITHUB_ENV

      - name: Set up Node.js
        uses: actions/setup-node@v6
        with:
          node-version: lts/*

      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          components: clippy, rustfmt
          override: true

      - name: Cache cargo registry
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: windows-x64-cargo-registry-${{ hashFiles('desktop/**/Cargo.lock') }}
          restore-keys: |
            windows-x64-cargo-registry-

      - name: Cache cargo build
        uses: actions/cache@v5
        with:
          path: desktop/target
          key: windows-x64-cargo-build-${{ hashFiles('desktop/**/Cargo.lock') }}
          restore-keys: |
            windows-x64-cargo-build-

      - name: Check formatting
        run: cargo fmt --all -- --check

      - name: Run clippy linter
        run: cargo clippy --all-targets --all-features -- -D warnings

      - name: Run tests
        run: cargo test --all

      - name: Cache node_modules
        uses: actions/cache@v5
        with:
          path: desktop/node_modules
          key: windows-x64-node-modules-${{ hashFiles('desktop/**/package-lock.json') }}
          restore-keys: |
            windows-x64-node-modules-

      - name: Install frontend dependencies
        run: npm ci

      - name: Lint frontend code
        run: npm run lint

      - name: Type check
        run: ./node_modules/.bin/tsc --noEmit

      - name: Run frontend tests
        run: npm run test -- --watch=false
