import puppeteer from '/home/moonui/public_html/presentation/albassit/node_modules/puppeteer-core/lib/esm/puppeteer/puppeteer-core.js';
import { mkdirSync } from 'fs';

const appUrl = 'https://moonui.elbaset.com/app';
const out = '/home/moonui/public_html/presentation/screens';
mkdirSync(out, { recursive: true });

const browser = await puppeteer.launch({
    headless: 'new',
    args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage', '--disable-gpu'],
    executablePath: '/root/.cache/puppeteer/chrome/linux-146.0.7680.153/chrome-linux64/chrome'
});

const page = await browser.newPage();
await page.setViewport({ width: 1366, height: 800 });

// Login
console.log('Logging in...');
await page.goto(`${appUrl}/auth/login`, { waitUntil: 'networkidle2', timeout: 30000 });
await new Promise(r => setTimeout(r, 3000));
await page.type('input[type="email"], input[formcontrolname="email"]', 'hazem@gt4it.com');
await page.type('input[type="password"], input[formcontrolname="password"]', '123456789');
await page.click('button[type="submit"]');
await new Promise(r => setTimeout(r, 6000));

async function shot(name, path) {
    try {
        await page.goto(`${appUrl}${path}`, { waitUntil: 'networkidle0', timeout: 25000 });
        await new Promise(r => setTimeout(r, 5000));
        // Check if we're on the right page or fell back to dashboard
        const url = page.url();
        await page.screenshot({ path: `${out}/${name}.png`, fullPage: false });
        console.log(`✓ ${name} → ${url.split('/app')[1] || '/'}`);
    } catch(e) {
        // Try networkidle2 fallback
        try {
            await page.goto(`${appUrl}${path}`, { waitUntil: 'networkidle2', timeout: 20000 });
            await new Promise(r => setTimeout(r, 5000));
            await page.screenshot({ path: `${out}/${name}.png`, fullPage: false });
            console.log(`✓ ${name} (retry)`);
        } catch(e2) {
            console.log(`✗ ${name}: ${e2.message.substring(0, 60)}`);
        }
    }
}

// Dashboard
await page.screenshot({ path: `${out}/dashboard.png`, fullPage: false });
console.log('✓ dashboard');

// Core
await shot('products', '/core/products');
await shot('partners', '/core/partners');
await shot('roles', '/core/roles');
await shot('settings', '/core/settings');

// Accounting
await shot('accounts', '/accounting/chart-of-accounts');
await shot('journal-entries', '/accounting/journal-entries');
await shot('bank-accounts', '/accounting/bank-accounts');
await shot('checks-received', '/accounting/checks-received');
await shot('financial-reports', '/accounting/reports');

// Sales
await shot('sales-invoices', '/sales/invoices');
await shot('sales-quotations', '/sales/quotations');

// Purchases
await shot('purchase-orders', '/purchases/orders');
await shot('purchase-bills', '/purchases/bills');

// Inventory
await shot('warehouses', '/inventory/warehouses');
await shot('stock-balances', '/inventory/stock-balances');

// HRM
await shot('employees', '/hrm/employees');
await shot('departments', '/hrm/departments');

// POS - skip, timeout issue
// await shot('pos', '/pos');

// LIS
await shot('lis-requests', '/lis/requests');
await shot('lis-investigations', '/lis/investigations');

await browser.close();
console.log('Done!');
