"""Pull samples from VASLOG, VASLOG - Engineering Tested, and Corrected HVAS Files."""
import paramiko, base64, os

import subprocess, yaml as _yaml

HOST='192.168.0.6'; USER='sysadmin'

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

PWD = _pwd()
LOCAL = r'D:\claudetools\projects\dataforth-dos\datasheet-pipeline\scmvas-hvas-research\samples'
os.makedirs(LOCAL, exist_ok=True)
os.makedirs(os.path.join(LOCAL, 'vaslog-dat'), exist_ok=True)
os.makedirs(os.path.join(LOCAL, 'vaslog-engtxt'), exist_ok=True)
os.makedirs(os.path.join(LOCAL, 'corrected-hvas'), exist_ok=True)

def ps(c, cmd, to=120):
    enc = base64.b64encode(cmd.encode('utf-16-le')).decode()
    stdin, stdout, stderr = c.exec_command(f'powershell -NoProfile -EncodedCommand {enc}', timeout=to)
    return stdout.read().decode('utf-8', errors='replace')

c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, username=USER, password=PWD, timeout=30, look_for_keys=False, allow_agent=False)
sftp = c.open_sftp()

try:
    # List the Engineering-Tested TXT folder
    print('=== VASLOG - Engineering Tested listing ===')
    out = ps(c, r'Get-ChildItem "C:\Shares\test\TS-3R\LOGS\VASLOG\VASLOG - Engineering Tested" -Force | Select Name,Length,LastWriteTime | Format-Table -AutoSize | Out-String')
    print(out)

    # Pull all .DAT files from VASLOG (they're small, total ~250KB)
    print('\n=== Pulling VASLOG .DAT files ===')
    for name in ['HVAS-M01.DAT','HVAS-M02.DAT','HVAS-M03.DAT','HVAS-M04.DAT','HVAS-MPT.DAT',
                 'VAS-M100.DAT','VAS-M200.DAT','VAS-M300.DAT','VAS-M400.DAT',
                 'VAS-M500.DAT','VAS-M600.DAT','VAS-M650.DAT','VAS-M700.DAT','VAS-MPT.DAT']:
        src = f'C:/Shares/test/TS-3R/LOGS/VASLOG/{name}'
        dst = os.path.join(LOCAL, 'vaslog-dat', name)
        try:
            sftp.get(src, dst)
            print(f'  pulled {name}')
        except Exception as e:
            print(f'  MISS {name}: {e}')

    # Pull 5 sample Engineering Tested TXTs
    print('\n=== Pulling VASLOG Engineering Tested TXTs (first 10) ===')
    engtxt_dir_posix = 'C:/Shares/test/TS-3R/LOGS/VASLOG/VASLOG - Engineering Tested'
    try:
        entries = sftp.listdir(engtxt_dir_posix)
        print(f'  {len(entries)} entries, pulling first 10')
        for name in entries[:10]:
            try:
                sftp.get(f'{engtxt_dir_posix}/{name}', os.path.join(LOCAL, 'vaslog-engtxt', name))
                print(f'  pulled {name}')
            except Exception as e:
                print(f'  MISS {name}: {e}')
    except Exception as e:
        print(f'  LIST FAIL: {e}')

    # Pull 5 Corrected HVAS TXT samples
    print('\n=== Pulling Corrected HVAS samples ===')
    ch_dir_posix = 'C:/Shares/test/Corrected HVAS Files'
    try:
        entries = sorted(sftp.listdir(ch_dir_posix))
        print(f'  {len(entries)} entries, pulling first 5')
        for name in entries[:5]:
            try:
                sftp.get(f'{ch_dir_posix}/{name}', os.path.join(LOCAL, 'corrected-hvas', name))
                print(f'  pulled {name}')
            except Exception as e:
                print(f'  MISS {name}: {e}')
    except Exception as e:
        print(f'  LIST FAIL: {e}')

finally:
    sftp.close()
    c.close()

print('\n=== DONE ===')
