import paramiko

c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect('160.187.143.253', username='root', password=';ur7n)LC1BQ;')

script = """
import { db } from './drizzle/db';
import { tenants, users } from './drizzle/schema';
import { eq } from 'drizzle-orm';
import { QuantumOrchestrator } from './lib/quantum-orchestrator';

async function test() {
    const userData = await db.select().from(users).where(eq(users.email, 'keanu@pc24.id')).limit(1);
    const user = userData[0];
    const tenantData = await db.select().from(tenants).where(eq(tenants.id, user.tenantId)).limit(1);
    const tenant = tenantData[0];
    
    const rawLicenses = tenant.licenses || '{}';
    const tenantLicParsed = JSON.parse(rawLicenses);
    
    let tenantLicNormalized: Record<string, string> = {};
    if (Array.isArray(tenantLicParsed)) {
      tenantLicParsed.forEach((k: string) => tenantLicNormalized[k] = 'GRANTED');
    } else {
      tenantLicNormalized = tenantLicParsed || {};
    }

    const userLicParsed = JSON.parse(user.licenses || '{}');
    let userLicNormalized: Record<string, string> = {};
    if (Array.isArray(userLicParsed)) {
      userLicParsed.forEach((k: string) => userLicNormalized[k] = 'GRANTED');
    } else {
      userLicNormalized = userLicParsed || {};
    }
    
    const capabilities = QuantumOrchestrator.resolve(
      tenantLicNormalized,
      userLicNormalized,
      !!tenant.isActive
    );
    
    console.log("UI MATRIX FOR KEANU:");
    console.log(JSON.stringify(capabilities.ui, null, 2));
    process.exit(0);
}
test();
"""

def r(cmd):
    print(f"Executing: {cmd}")
    _, so, se = c.exec_command(cmd)
    stdout = so.read().decode('utf-8', 'replace').strip()
    stderr = se.read().decode('utf-8', 'replace').strip()
    if stdout: print(f"STDOUT:\n{stdout}")
    if stderr: print(f"STDERR:\n{stderr}")
    return stdout

# Upload script to VPS
sftp = c.open_sftp()
with sftp.file('/var/www/iam/test_quantum.ts', 'w') as f:
    f.write(script)
sftp.close()

r("cd /var/www/iam && npx tsx test_quantum.ts")

c.close()
