name: Build and Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    runs-on: windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Stamp version from tag
        shell: powershell
        run: |
          $tag = "${{ github.ref_name }}"
          $version = $tag.TrimStart('v')
          Write-Host "Stamping version: $version"

          # Update Cargo.toml (first occurrence of version = "x.y.z" in [package])
          $cargo = Get-Content src-tauri\Cargo.toml -Raw
          $cargo = $cargo -replace '(?m)^version = "\d+\.\d+\.\d+"', "version = `"$version`""
          Set-Content src-tauri\Cargo.toml $cargo

          # Update tauri.conf.json
          $conf = Get-Content src-tauri\tauri.conf.json -Raw | ConvertFrom-Json
          $conf.version = $version
          $conf | ConvertTo-Json -Depth 10 | Set-Content src-tauri\tauri.conf.json

      - name: Install npm dependencies
        shell: powershell
        run: npm install

      - name: Build C# sidecar
        shell: powershell
        run: |
          cd sidecar
          dotnet publish -c Release -r win-x64 --self-contained

      - name: Copy sidecar to Tauri binaries
        shell: powershell
        run: |
          New-Item -ItemType Directory -Force -Path src-tauri\binaries
          $src = "sidecar\bin\Release\net8.0\win-x64\publish\asw-sidecar.exe"
          $dst = "src-tauri\binaries\asw-sidecar-x86_64-pc-windows-msvc.exe"
          Copy-Item $src $dst
          Write-Host "Copied sidecar: $dst"

      - name: Build Tauri app
        shell: powershell
        run: npm run tauri build

      - name: Create Gitea Release
        id: create_release
        shell: powershell
        run: |
          $tag = "${{ github.ref_name }}"
          $body = @{
            tag_name   = $tag
            name       = "Artificer's Scrollwork $tag"
            body       = "Automated release $tag"
            draft      = $false
            prerelease = $false
          } | ConvertTo-Json

          $response = Invoke-RestMethod `
            -Uri "https://gitea.whitlocktech.com/api/v1/repos/whitlocktech/Artificers-Scrollwork/releases" `
            -Method Post `
            -Headers @{
              Authorization  = "token ${{ secrets.RELEASE_TOKEN }}"
              "Content-Type" = "application/json"
            } `
            -Body $body

          $releaseId = $response.id
          Write-Host "Created release ID: $releaseId"
          Add-Content -Path $env:GITHUB_OUTPUT -Value "release_id=$releaseId"

      - name: Upload MSI
        shell: powershell
        run: |
          $releaseId = "${{ steps.create_release.outputs.release_id }}"
          $msi = Get-ChildItem -Path "src-tauri\target\release\bundle\msi\*.msi" | Select-Object -First 1

          Write-Host "MSI: $($msi.FullName)"

          if (!(Test-Path $msi.FullName)) {
              Write-Error "MSI not found in src-tauri\target\release\bundle\msi\"
              exit 1
          }

          $uri = "https://gitea.whitlocktech.com/api/v1/repos/whitlocktech/Artificers-Scrollwork/releases/$releaseId/assets?name=$($msi.Name)"

          & curl.exe -X POST `
            -H "Authorization: token ${{ secrets.RELEASE_TOKEN }}" `
            -F "attachment=@$($msi.FullName)" `
            "$uri"
