name: Windows Nightly

on:
  schedule:
    - cron: '0 2 * * *'
  workflow_dispatch:

jobs:
  windows-nightly:
    # Disabled until a self-hosted runner with label "windows-runner" is registered.
    name: Build & Deploy Windows (Nightly)
    runs-on: windows-runner
    timeout-minutes: 90
    if: false

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Check for recent changes on main
        run: |
          $changes = git log --oneline --since "24 hours ago" origin/main
          if (-not $changes) {
            Write-Output "No changes in last 24 hours, skipping build."
            Add-Content -Path $env:GITHUB_ENV -Value "SKIP_BUILD=true"
          }

      - name: Build Windows
        if: env.SKIP_BUILD != 'true'
        run: task build-windows-release

      - name: Set up SSH key
        if: env.SKIP_BUILD != 'true'
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
        run: |
          mkdir -p $env:USERPROFILE\.ssh
          $env:SSH_PRIVATE_KEY | Out-File -FilePath "$env:USERPROFILE\.ssh\id_rsa" -Encoding ascii
          icacls "$env:USERPROFILE\.ssh\id_rsa" /inheritance:r /grant:r "$env:USERNAME:F"

      - name: Deploy Windows to server
        if: env.SKIP_BUILD != 'true'
        env:
          SSH_USER: ${{ secrets.SSH_USER }}
          SSH_HOST: ${{ secrets.SSH_HOST }}
        run: task deploy-windows-to-server
