"""Probe DFWDS folder on AD1 via AD2."""
import base64, paramiko, subprocess, yaml

pwd_raw = yaml.safe_load(subprocess.run(
    ['sops','-d','D:/vault/clients/dataforth/ad2.sops.yaml'],
    capture_output=True, text=True, timeout=30, check=True,
).stdout)['credentials']['password']
PWD = pwd_raw.replace('\\', '')

c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect('192.168.0.6', username='sysadmin', password=PWD,
          timeout=30, banner_timeout=45, look_for_keys=False, allow_agent=False)

def ps(cmd, to=180):
    enc = base64.b64encode(cmd.encode('utf-16-le')).decode()
    _, o, e = c.exec_command(f'powershell -NoProfile -EncodedCommand {enc}', timeout=to)
    return o.read().decode('utf-8','replace'), e.read().decode('utf-8','replace')

BASE = r'\\AD1\Engineering\ENGR\ATE\Test Datasheets\DFWDS'

print(f'=== top-level: {BASE} ===')
out, err = ps(f'Get-ChildItem -LiteralPath "{BASE}" -Force | Select Name,Mode,Length,LastWriteTime | Format-Table -AutoSize | Out-String')
print(out)

print('\n=== recursive count + total bytes ===')
out, err = ps(f'$f = Get-ChildItem -LiteralPath "{BASE}" -Recurse -File -ErrorAction SilentlyContinue; "files: $($f.Count)"; "bytes: $(($f | Measure-Object Length -Sum).Sum)"')
print(out)

print('\n=== full recursive listing (first 100) ===')
out, err = ps(f'Get-ChildItem -LiteralPath "{BASE}" -Recurse -File -ErrorAction SilentlyContinue | Select-Object -First 100 FullName,Length,LastWriteTime | Format-Table -AutoSize | Out-String')
print(out[:6000])

c.close()
