#Requires -Version 7.0
<#
.SYNOPSIS
  End-to-end CLI test for the AB Legacy (PCCC) driver.

.DESCRIPTION
  Runs against libplctag's ab_server PCCC Docker fixture (one of the
  slc500 / micrologix / plc5 compose profiles) or real SLC / MicroLogix /
  PLC-5 hardware. Five assertions: probe / driver-loopback / forward-
  bridge / reverse-bridge / subscribe-sees-change.

  ab_server enforces a non-empty CIP routing path (`/1,0`) before the
  PCCC dispatcher runs; real hardware accepts an empty path. The default
  $Gateway uses `/1,0` for the Docker fixture — pass `-Gateway
  "ab://host:44818/"` when pointing at a real SLC 5/05 / MicroLogix /
  PLC-5.

.PARAMETER Gateway
  ab://host[:port]/cip-path. Default ab://127.0.0.1/1,0 (Docker fixture).

.PARAMETER PlcType
  Slc500 / MicroLogix / Plc5 / LogixPccc (default Slc500).

.PARAMETER Address
  PCCC address to exercise. Default N7:5.

.PARAMETER OpcUaUrl
  OtOpcUa server endpoint.

.PARAMETER BridgeNodeId
  NodeId at which the server publishes the Address.
#>

param(
    [string]$Gateway = "ab://127.0.0.1/1,0",
    [string]$PlcType = "Slc500",
    [string]$Address = "N7:5",
    [string]$OpcUaUrl = "opc.tcp://localhost:4840",
    [Parameter(Mandatory)] [string]$BridgeNodeId
)

$ErrorActionPreference = "Stop"
. "$PSScriptRoot/_common.ps1"

# ab_server PCCC works; the earlier "upstream-broken" gate is gone. The only
# caveat: libplctag's ab_server rejects empty CIP paths, so $Gateway must
# carry a non-empty path segment (default /1,0). Real SLC/PLC-5 hardware
# accepts an empty path — use `ab://host:44818/` when pointing at real PLCs.

$abLegacyCli = Get-CliInvocation `
    -ProjectFolder "src/Drivers/Cli/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Cli" `
    -ExeName "otopcua-ablegacy-cli"
$opcUaCli = Get-CliInvocation `
    -ProjectFolder "src/Client/ZB.MOM.WW.OtOpcUa.Client.CLI" `
    -ExeName "otopcua-cli"

$commonAbLegacy = @("-g", $Gateway, "-P", $PlcType)
$results = @()

$results += Test-Probe `
    -Cli $abLegacyCli `
    -ProbeArgs (@("probe") + $commonAbLegacy + @("-a", "N7:0"))

$writeValue = Get-Random -Minimum 1 -Maximum 9999
$results += Test-DriverLoopback `
    -Cli $abLegacyCli `
    -WriteArgs (@("write") + $commonAbLegacy + @("-a", $Address, "-t", "Int", "-v", $writeValue)) `
    -ReadArgs  (@("read")  + $commonAbLegacy + @("-a", $Address, "-t", "Int")) `
    -ExpectedValue "$writeValue"

$bridgeValue = Get-Random -Minimum 10000 -Maximum 19999
$results += Test-ServerBridge `
    -DriverCli $abLegacyCli `
    -DriverWriteArgs (@("write") + $commonAbLegacy + @("-a", $Address, "-t", "Int", "-v", $bridgeValue)) `
    -OpcUaCli $opcUaCli `
    -OpcUaUrl $OpcUaUrl `
    -OpcUaNodeId $BridgeNodeId `
    -ExpectedValue "$bridgeValue"

$reverseValue = Get-Random -Minimum 20000 -Maximum 29999
$results += Test-OpcUaWriteBridge `
    -OpcUaCli $opcUaCli `
    -OpcUaUrl $OpcUaUrl `
    -OpcUaNodeId $BridgeNodeId `
    -DriverCli $abLegacyCli `
    -DriverReadArgs (@("read") + $commonAbLegacy + @("-a", $Address, "-t", "Int")) `
    -ExpectedValue "$reverseValue"

$subValue = Get-Random -Minimum 30000 -Maximum 32766
$results += Test-SubscribeSeesChange `
    -OpcUaCli $opcUaCli `
    -OpcUaUrl $OpcUaUrl `
    -OpcUaNodeId $BridgeNodeId `
    -DriverCli $abLegacyCli `
    -DriverWriteArgs (@("write") + $commonAbLegacy + @("-a", $Address, "-t", "Int", "-v", $subValue)) `
    -ExpectedValue "$subValue"

Write-Summary -Title "AB Legacy e2e" -Results $results
if ($results | Where-Object { -not $_.Passed }) { exit 1 }
