name: Build Windows

concurrency:
  group: build-windows-${{ github.ref }}${{ inputs.version && format('-{0}', github.run_id) || '' }}
  cancel-in-progress: true

on:
  push:
    branches: [master, main]
  pull_request:
    branches: [master, main]
  workflow_dispatch:
  workflow_call:
    inputs:
      version:
        description: "Release version (e.g. 1.0.0)"
        required: true
        type: string

jobs:
  build:
    name: Build
    runs-on: [self-hosted, Windows]

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Rust version
        shell: powershell
        run: rustc --version; cargo --version

      - name: Set version
        if: inputs.version
        shell: powershell
        run: |
          (Get-Content Cargo.toml) -replace '^version = .*', "version = `"${{ inputs.version }}`"" | Set-Content Cargo.toml

      - name: Check source cache
        id: cache
        shell: powershell
        run: |
          $srcFiles = Get-ChildItem -Path "src" -Recurse -Include "*.rs" | Sort-Object FullName
          $configFiles = @("Cargo.toml", "Cargo.lock") | Where-Object { Test-Path $_ } | Get-Item
          $allFiles = @($srcFiles) + @($configFiles) | Where-Object { $_ }
          $hashInput = ($allFiles | ForEach-Object { (Get-FileHash $_.FullName -Algorithm SHA256).Hash }) -join ""
          $sourceHash = (Get-FileHash -InputStream ([IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes($hashInput))) -Algorithm SHA256).Hash.Substring(0, 12)

          $cacheDir = "C:\builds\.cache\telegram-bot-mcp"
          $cachedExe = "$cacheDir\telegram-bot-mcp-$sourceHash.exe"

          Write-Host "Source hash: $sourceHash"
          echo "hash=$sourceHash" >> $env:GITHUB_OUTPUT

          if (Test-Path $cachedExe) {
            Write-Host "Cache HIT"
            echo "hit=true" >> $env:GITHUB_OUTPUT
          } else {
            Write-Host "Cache MISS"
            echo "hit=false" >> $env:GITHUB_OUTPUT
          }

      - name: Build (release)
        if: steps.cache.outputs.hit != 'true'
        run: cargo build --release

      - name: Save to cache
        if: steps.cache.outputs.hit != 'true'
        shell: powershell
        run: |
          $cacheDir = "C:\builds\.cache\telegram-bot-mcp"
          New-Item -ItemType Directory -Force -Path $cacheDir | Out-Null
          Copy-Item "target\release\telegram-bot-mcp.exe" "$cacheDir\telegram-bot-mcp-${{ steps.cache.outputs.hash }}.exe"
          Get-ChildItem "$cacheDir\telegram-bot-mcp-*.exe" | Sort-Object LastWriteTime -Descending | Select-Object -Skip 5 | Remove-Item -Force

      - name: Prepare artifact
        if: inputs.version
        shell: powershell
        run: |
          if ("${{ steps.cache.outputs.hit }}" -eq "true") {
            $bin = "C:\builds\.cache\telegram-bot-mcp\telegram-bot-mcp-${{ steps.cache.outputs.hash }}.exe"
          } else {
            $bin = "target\release\telegram-bot-mcp.exe"
          }
          New-Item -ItemType Directory -Force -Path dist | Out-Null
          Copy-Item $bin "dist\telegram-bot-mcp-v${{ inputs.version }}-windows-x86_64.exe"

      - name: Upload artifact
        if: inputs.version
        uses: actions/upload-artifact@v4
        with:
          name: binary-windows
          path: dist/*
          retention-days: 1

      - name: Verify
        shell: powershell
        run: |
          if ("${{ steps.cache.outputs.hit }}" -eq "true") {
            $bin = "C:\builds\.cache\telegram-bot-mcp\telegram-bot-mcp-${{ steps.cache.outputs.hash }}.exe"
          } else {
            $bin = "target\release\telegram-bot-mcp.exe"
          }
          $size = (Get-Item $bin).Length / 1MB
          Write-Host ("Binary: {0:N1} MB" -f $size)
          & $bin list | Select-Object -Last 1
