import puppeteer from 'puppeteer-core';
import { mkdirSync } from 'fs';

const base = 'https://siam.technoogate.com/erp/controllers';
const out = '/home/moonui/public_html/presentation/albassit/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: 900 });

// Login
console.log('Logging in...');
await page.goto(`${base}/login.php`, { waitUntil: 'networkidle2', timeout: 30000 });
await new Promise(r => setTimeout(r, 2000));
await page.type('#username', 'admin');
await page.type('#password', 'zizo123*');
try { await page.select('select', 'siam_erp'); } catch(e) {}
await page.evaluate(() => { document.querySelector('form').submit(); });
await page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 30000 }).catch(() => {});
await new Promise(r => setTimeout(r, 4000));

// Helper: scroll to content area and clip screenshot (skip menu)
async function takeContentScreenshot(name, url, scrollY = 750) {
    try {
        await page.goto(`${base}/${url}`, { waitUntil: 'networkidle2', timeout: 20000 });
        await new Promise(r => setTimeout(r, 3000));

        // Scroll past the menu
        await page.evaluate((y) => window.scrollTo(0, y), scrollY);
        await new Promise(r => setTimeout(r, 1000));

        // Take screenshot of viewport (which now shows content area)
        await page.screenshot({
            path: `${out}/${name}.png`,
            fullPage: false,
            clip: {
                x: 0,
                y: scrollY,
                width: 1366,
                height: 700
            }
        });

        console.log(`✓ ${name}`);
    } catch(e) {
        console.log(`✗ ${name}: ${e.message.substring(0, 80)}`);
    }
}

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

// Sell bill - scroll past menu to show invoice list/form
await takeContentScreenshot('sell-bill', 'sellbillController.php?do=show', 750);
await takeContentScreenshot('sell-bill-form', 'sellbillController.php', 750);
await takeContentScreenshot('buy-bill', 'buyBillController.php?do=show', 750);
await takeContentScreenshot('store-detail', 'storedetailController.php?do=show', 750);
await takeContentScreenshot('accounts-tree', 'accountstree.php', 750);
await takeContentScreenshot('clients-show', 'clientController.php?do=show', 750);
await takeContentScreenshot('supplier-show', 'supplierController.php?do=show', 750);
await takeContentScreenshot('save-show', 'saveController.php?do=show', 750);
await takeContentScreenshot('expenses-show', 'expensesController.php?do=show', 750);
await takeContentScreenshot('balance-report', 'balancereportController.php', 750);
await takeContentScreenshot('settings', 'programsettingsController.php', 750);
await takeContentScreenshot('cashflow', 'cashflowsController.php', 750);
await takeContentScreenshot('users', 'userController.php?do=show', 750);

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