$ErrorActionPreference = "Stop"

$root = Split-Path -Parent $PSScriptRoot
$mcpDir = Join-Path $root "smqjh-mcp-server"
$runScript = Join-Path $mcpDir "run.ps1"
$logsDir = Join-Path $root "logs"

if (-not (Test-Path $runScript)) {
  throw "MCP run script not found: $runScript"
}

New-Item -ItemType Directory -Force -Path $logsDir | Out-Null
$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
$stdout = Join-Path $logsDir "mcp-$stamp.out.log"
$stderr = Join-Path $logsDir "mcp-$stamp.err.log"

$command = "& '$runScript'"

Start-Process `
  -FilePath "powershell" `
  -ArgumentList @("-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", $command) `
  -WorkingDirectory $mcpDir `
  -WindowStyle Hidden `
  -RedirectStandardOutput $stdout `
  -RedirectStandardError $stderr

Write-Host "MCP start requested."
Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"
