import { chromium } from 'playwright';
const b = await chromium.launch();
const p = await b.newPage({ viewport: { width: 1440, height: 1000 } });
await p.goto('http://127.0.0.1:8200/login', { waitUntil: 'networkidle' });
await p.fill('input[name=email]','admin@brokaminternational.com');
await p.fill('input[name=password]','password');
await p.click('button[type=submit]');
await p.waitForURL('**/dashboard', { waitUntil: 'networkidle' });

// Quotation create form (line editor)
await p.goto('http://127.0.0.1:8200/quotations/create', { waitUntil: 'networkidle' });
await p.waitForTimeout(500);
await p.screenshot({ path: 'C:/tmp/p4_quote_form.png', fullPage: true });

// Quotation show (seeded demo)
await p.goto('http://127.0.0.1:8200/quotations', { waitUntil: 'networkidle' });
const row = await p.getAttribute('tbody tr', 'onclick');
const m = row && row.match(/'([^']+)'/);
await p.goto(m[1], { waitUntil: 'networkidle' });
await p.waitForTimeout(400);
await p.screenshot({ path: 'C:/tmp/p4_quote_show.png', fullPage: true });

// Accept the quote, then raise a sales order
await p.click('form[action$="/accept"] button');
await p.waitForLoadState('networkidle');
await p.waitForTimeout(300);
// fill PO + create order
await p.fill('input[name=po_reference]', 'PO-DEMO-001');
await p.click('form[action$="/sales-order"] button');
await p.waitForLoadState('networkidle');
await p.waitForTimeout(400);
await p.screenshot({ path: 'C:/tmp/p4_sales_order.png', fullPage: true });
console.log('done; final url', p.url());
await b.close();
