param(
  [Parameter(Mandatory = $true)]
  [string]$RunnerToken,

  [string]$RunnerRoot = 'C:\xiaoxia-ci\act_runner',
  [string]$InstanceUrl = 'https://api.xiaoxiajianji.com/git',
  [string]$RunnerName = 'xiaoxia-windows-runner'
)

$ErrorActionPreference = 'Stop'

$runnerExe = Join-Path $RunnerRoot 'act_runner.exe'
$configPath = Join-Path $RunnerRoot 'config.yaml'
$workDir = Join-Path $RunnerRoot 'work'
$logDir = Join-Path $RunnerRoot 'logs'
$scriptDir = Join-Path $RunnerRoot 'scripts'

New-Item -ItemType Directory -Force -Path $RunnerRoot, $workDir, $logDir, $scriptDir | Out-Null

if (-not (Test-Path $runnerExe)) {
  throw "Missing runner executable: $runnerExe"
}

$config = @"
instance:
  url: $InstanceUrl
  token: $RunnerToken

runner:
  name: $RunnerName
  labels:
    - windows
    - xiaoxia-ci
    - local
  workdir: $workDir
"@

Set-Content -Path $configPath -Value $config -Encoding UTF8

Write-Host "Runner config written: $configPath"
Write-Host "Next step: register/start runner with the official act_runner command for this binary version."
