# QwenClaw Rig Service Startup Script (PowerShell)
# Starts the Rig AI agent service

$ErrorActionPreference = "Stop"

$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RigDir = Join-Path $ScriptDir "..\rig-service"

Write-Host "🦀 Starting QwenClaw Rig Service..." -ForegroundColor Cyan

Set-Location $RigDir

# Check if built
if (-not (Test-Path "target\release\qwenclaw-rig.exe")) {
    Write-Host "⚠️  Rig service not built. Building now..." -ForegroundColor Yellow
    cargo build --release
}

# Load environment variables
$EnvFile = Join-Path $RigDir ".env"
if (Test-Path $EnvFile) {
    Get-Content $EnvFile | ForEach-Object {
        if ($_ -match '^\s*([^#=]+)\s*=\s*(.+)\s*$' -and $_ -notmatch '^#') {
            $name = $matches[1].Trim()
            $value = $matches[2].Trim()
            Set-Item -Force -Path "ENV:$name" -Value $value
        }
    }
}

# Set defaults if not set
if (-not $env:RIG_HOST) { $env:RIG_HOST = "127.0.0.1" }
if (-not $env:RIG_PORT) { $env:RIG_PORT = "8080" }

Write-Host "🚀 Starting Rig service on http://$($env:RIG_HOST):$($env:RIG_PORT)" -ForegroundColor Green

# Start service
cargo run --release
