import { chromium } from './node_modules/playwright/index.mjs';

const PVE_TOKEN = process.env.PVE_TOKEN; // format: root@pam!openclaw=<uuid>

const browser = await chromium.launch({
  executablePath: '/home/node/.cache/ms-playwright/chromium-1208/chrome-linux64/chrome',
  args: ['--no-sandbox', '--disable-dev-shm-usage', '--disable-gpu', '--ignore-certificate-errors']
});

const context = await browser.newContext({ ignoreHTTPSErrors: true });
const page = await context.newPage();
await page.setViewportSize({ width: 1440, height: 900 });

// Proxmox accepte le token API via cookie PVEAPIToken
// On forge l'auth cookie directement
const [user, tokenPart] = PVE_TOKEN.split('=', 1)[0].split('!');
const tokenValue = PVE_TOKEN.split('=').slice(1).join('=');
const tokenId = PVE_TOKEN.split('=')[0]; // root@pam!openclaw

await context.addCookies([{
  name: 'PVEAuthCookie',
  value: `PVEAPIToken=${PVE_TOKEN}`,
  domain: '192.168.1.250',
  path: '/'
}]);

// Naviguer directement sur Proxmox
await page.goto('https://192.168.1.250:8006', { waitUntil: 'networkidle', timeout: 20000 });
await page.waitForTimeout(4000);

// Screenshot page principale
await page.screenshot({ path: 'proxmox_main.png', fullPage: false });
console.log('✅ Screenshot principal pris');

await browser.close();
