"""Targeted Engineering-Tested .txt import — v2.

Drops a node script on AD2 that reads the directory itself and calls
importFiles() with the full list. Avoids CLI-length limits and chunking.
"""
import base64, subprocess, yaml, paramiko, sys

HOST = '192.168.0.6'
USER = 'sysadmin'

NODE_SCRIPT = r'''
const fs = require('fs');
const path = require('path');
const db = require('./database/db');
const { importFiles } = require('./database/import');

const DIR = 'C:\\Shares\\test\\TS-3R\\LOGS\\VASLOG\\VASLOG - Engineering Tested';

(async () => {
    const entries = fs.readdirSync(DIR).filter(n => n.toLowerCase().endsWith('.txt'));
    const files = entries.map(n => path.join(DIR, n));
    console.log('[INFO] ' + files.length + ' .txt files queued for import');
    const result = await importFiles(files);
    console.log('[DONE] imported=' + result.imported + ' parsed=' + result.total);

    const cnt = await db.queryOne("SELECT COUNT(*) c FROM test_records WHERE log_type='VASLOG_ENG'");
    console.log('[DB] VASLOG_ENG rows total: ' + cnt.c);

    // Check forweb export status
    const forweb = await db.queryOne(
        "SELECT COUNT(*) c FROM test_records WHERE log_type='VASLOG_ENG' AND forweb_exported_at IS NOT NULL"
    );
    console.log('[DB] VASLOG_ENG already on X:\\For_Web: ' + forweb.c);

    await db.close();
})().catch(e => { console.error('[FAIL] ' + e.message); process.exit(1); });
'''

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('\\', '')

def ps(c, cmd, to=1800):
    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', 'replace'), stderr.read().decode('utf-8', 'replace'), stdout.channel.recv_exit_status()

def main():
    c = paramiko.SSHClient()
    c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    c.connect(HOST, username=USER, password=pwd(), timeout=30, banner_timeout=45,
              look_for_keys=False, allow_agent=False)
    try:
        sftp = c.open_sftp()
        remote_js = 'C:/Shares/testdatadb/_import_engtxt.js'
        with sftp.open(remote_js, 'w') as fh:
            fh.write(NODE_SCRIPT)
        sftp.close()
        print(f'[OK] deployed {remote_js}', flush=True)

        print('[RUN] executing ./_import_engtxt.js (this may take a few minutes)', flush=True)
        out, err, rc = ps(c, r'cd C:\Shares\testdatadb; & node ./_import_engtxt.js')
        print(f'[rc={rc}]', flush=True)
        print(out, flush=True)
        if err.strip() and 'CLIXML' not in err:
            print(f'--- STDERR ---\n{err[:2000]}', flush=True)

        sftp = c.open_sftp()
        try:
            sftp.remove(remote_js)
            print(f'[OK] removed {remote_js}', flush=True)
        except Exception as e:
            print(f'[WARN] cleanup {remote_js}: {e}', flush=True)
        sftp.close()
    finally:
        c.close()

if __name__ == '__main__':
    main()
