# Primary install: copy Paychek sources into NinjaTrader Custom (close NT first).
# Optional: Tools > Import > NinjaScript Add-On... on PaychekSafeguard-Addon.zip
$ErrorActionPreference = "Stop"
$Here = $PSScriptRoot
$Custom = Join-Path $env:USERPROFILE "Documents\NinjaTrader 8\bin\Custom"
$Zip = Join-Path $Here "PaychekSafeguard-Addon.zip"

if (-not (Test-Path -LiteralPath $Custom)) {
    Write-Host "[ERROR] NinjaTrader Custom folder not found:" -ForegroundColor Red
    Write-Host "  $Custom"
    Write-Host "Install / open NinjaTrader 8 once, then retry."
    Read-Host "Press Enter to exit"
    exit 1
}
if (-not (Test-Path -LiteralPath $Zip)) {
    Write-Host "[ERROR] Missing $Zip" -ForegroundColor Red
    Read-Host "Press Enter to exit"
    exit 1
}

function Find-StagedFile {
    param([string]$Stage, [string]$FileName, [string[]]$Subdirs)
    foreach ($sub in $Subdirs) {
        $p = Join-Path (Join-Path $Stage $sub) $FileName
        if (Test-Path -LiteralPath $p) { return $p }
    }
    $flat = Join-Path $Stage $FileName
    if (Test-Path -LiteralPath $flat) { return $flat }
    return $null
}

Get-Process -Name "NinjaTrader*" -ErrorAction SilentlyContinue | ForEach-Object {
    Write-Host "Stopping $($_.ProcessName)..."
    Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue
}
Start-Sleep -Seconds 1

$stage = Join-Path $env:TEMP ("paychek-nt-" + [guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Force -Path $stage | Out-Null
try {
    Expand-Archive -LiteralPath $Zip -DestinationPath $stage -Force
    $addons = Join-Path $Custom "AddOns"
    $drawing = Join-Path $Custom "DrawingTools"
    New-Item -ItemType Directory -Force -Path $addons | Out-Null
    New-Item -ItemType Directory -Force -Path $drawing | Out-Null

    foreach ($name in @(
        "PaychekSafeguardAddOn.cs",
        "PaychekZoneChartToolbar.cs",
        "PaychekSafeguardZoneHelpers.cs",
        "PaychekChartTraderCloseGuard.cs"
    )) {
        $src = Find-StagedFile -Stage $stage -FileName $name -Subdirs @("AddOns")
        if ($src) {
            Copy-Item -LiteralPath $src -Destination (Join-Path $addons $name) -Force
            Write-Host "OK AddOns\$name" -ForegroundColor Green
        } else {
            Write-Host "[WARN] Missing $name in archive" -ForegroundColor Yellow
        }
    }
    $zone = Find-StagedFile -Stage $stage -FileName "PaychekSafeguardZone.cs" -Subdirs @("DrawingTools")
    if ($zone) {
        Copy-Item -LiteralPath $zone -Destination (Join-Path $drawing "PaychekSafeguardZone.cs") -Force
        Write-Host "OK DrawingTools\PaychekSafeguardZone.cs" -ForegroundColor Green
    } else {
        Write-Host "[WARN] Missing PaychekSafeguardZone.cs in archive" -ForegroundColor Yellow
    }
    $json = Find-StagedFile -Stage $stage -FileName "PaychekSafeguard.json" -Subdirs @("")
    if ($json) {
        Copy-Item -LiteralPath $json -Destination (Join-Path $Custom "PaychekSafeguard.json") -Force
        Write-Host "OK PaychekSafeguard.json" -ForegroundColor Green
    }
}
finally {
    Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue
}

Write-Host ""
Write-Host "Next: reopen NinjaTrader -> New -> NinjaScript Editor -> Compile (F5)" -ForegroundColor Cyan
Write-Host "Then: Control Center -> New -> Paychek Safeguard"
Read-Host "Press Enter to close"
