"""Check X: drive folders to see what DFWDS would have to process."""
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=120):
    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')

paths = [
    r'\\ad2\webshare\Test_Datasheets',
    r'\\ad2\webshare\Bad_Datasheets',
    r'\\ad2\webshare\For_Web',
    r'\\ad2\webshare\Datasheets_Log',
]
for p in paths:
    print(f'\n=== {p} ===')
    out, err = ps(f'''
if (Test-Path "{p}") {{
    $f = Get-ChildItem -LiteralPath "{p}" -File -ErrorAction SilentlyContinue
    "files: $($f.Count)"
    if ($f.Count -gt 0) {{
        "bytes: $(($f | Measure-Object Length -Sum).Sum)"
        "oldest: $(($f | Sort-Object LastWriteTime | Select-Object -First 1).LastWriteTime)"
        "newest: $(($f | Sort-Object LastWriteTime | Select-Object -Last 1).LastWriteTime)"
        "first 5 by name:"
        $f | Sort-Object Name | Select-Object -First 5 Name,Length,LastWriteTime | Format-Table -AutoSize | Out-String
        "newest 5:"
        $f | Sort-Object LastWriteTime -Descending | Select-Object -First 5 Name,Length,LastWriteTime | Format-Table -AutoSize | Out-String
    }}
}} else {{ "PATH MISSING" }}
''')
    print(out.rstrip())
    if err.strip() and 'CLIXML' not in err: print('[stderr]', err[:200])

c.close()
