"""Post-backfill verification: counts + sample the 438 skipped records."""
import base64, subprocess, yaml, paramiko

NODE_SCRIPT = r'''
const db = require('./database/db');
(async () => {
    const before = await db.queryOne(
        "SELECT COUNT(*) c FROM test_records WHERE overall_result='PASS' AND forweb_exported_at IS NULL " +
        "AND ((model_number LIKE 'SCMVAS%' OR model_number LIKE 'SCMHVAS%' OR model_number LIKE 'VAS-M%' OR model_number LIKE 'HVAS-M%') OR log_type='VASLOG_ENG')"
    );
    console.log('SCMVAS/SCMHVAS backlog remaining: ' + before.c);

    const exported = await db.queryOne(
        "SELECT COUNT(*) c FROM test_records WHERE forweb_exported_at IS NOT NULL " +
        "AND ((model_number LIKE 'SCMVAS%' OR model_number LIKE 'SCMHVAS%') OR log_type='VASLOG_ENG')"
    );
    console.log('SCMVAS/SCMHVAS exported total:   ' + exported.c);

    // Sample of skipped model names
    console.log('');
    console.log('Skipped-record model breakdown:');
    const skipped = await db.query(
        "SELECT model_number, log_type, COUNT(*) c FROM test_records WHERE overall_result='PASS' AND forweb_exported_at IS NULL " +
        "AND ((model_number LIKE 'SCMVAS%' OR model_number LIKE 'SCMHVAS%' OR model_number LIKE 'VAS-M%' OR model_number LIKE 'HVAS-M%') OR log_type='VASLOG_ENG') " +
        "GROUP BY model_number, log_type ORDER BY c DESC LIMIT 30"
    );
    for (const r of skipped) console.log('  ' + r.model_number.padEnd(20) + ' ' + (r.log_type||'').padEnd(12) + ' ' + r.c);

    await db.close();
})();
'''

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=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','replace'), stderr.read().decode('utf-8','replace'), stdout.channel.recv_exit_status()

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)
try:
    sftp = c.open_sftp()
    remote = 'C:/Shares/testdatadb/_final_verify.js'
    with sftp.open(remote,'w') as fh: fh.write(NODE_SCRIPT)
    sftp.close()

    out, err, rc = ps(c, r'cd C:\Shares\testdatadb; & node ./_final_verify.js')
    print(out)
    if err.strip() and 'CLIXML' not in err:
        print('STDERR:', err[:400])

    # Count For_Web files
    print('\n=== For_Web file count ===')
    out, err, rc = ps(c, r'(Get-ChildItem "\\ad2\webshare\For_Web" -File -Filter *.TXT | Measure-Object).Count')
    print('Total *.TXT in For_Web: ' + out.strip())

    sftp = c.open_sftp()
    try: sftp.remove(remote)
    except Exception: pass
    sftp.close()
finally:
    c.close()
