const { chromium } = require('@playwright/test');
const path = require('path');
const fs = require('fs');

const BASE_URL = 'http://localhost:8045';
const DIR = '/mnt/c/project/jongryangje/screenshots';

async function run() {
  if (!fs.existsSync(DIR)) fs.mkdirSync(DIR, { recursive: true });
  const browser = await chromium.launch();
  const page = await (await browser.newContext({ viewport: { width: 1440, height: 900 }, locale: 'ko-KR' })).newPage();

  // Super Admin 로그인 + 지자체 선택
  await page.goto(`${BASE_URL}/login`);
  await page.fill('input[name="login_id"]', 'tester_admin');
  await page.fill('input[name="password"]', 'test1234!');
  await page.click('button[type="submit"]');
  await page.waitForURL(url => !url.pathname.includes('/login'), { timeout: 30000 });
  await page.locator('input[name="lg_idx"]').first().check();
  await page.click('button[type="submit"]');
  await page.waitForURL(url => !url.pathname.includes('select-local-government'), { timeout: 30000 });

  const pages = [
    // Phase 2
    ['30_bag_price_create', '/bag/bag-prices/create'],
    ['31_packaging_unit', '/bag/packaging-units/manage'],
    ['32_sales_agency', '/bag/sales-agencies'],
    ['33_sales_agency_create', '/bag/sales-agencies/create'],
    ['34_manager', '/bag/managers'],
    ['35_manager_create', '/bag/managers/create'],
    ['36_company', '/bag/companies'],
    ['37_company_create', '/bag/companies/create'],
    ['38_free_recipient', '/bag/free-recipients'],
    ['39_free_recipient_create', '/bag/free-recipients/create'],
    ['40_lg_edit', '/admin/local-governments/edit/1'],
    ['41_password_change', '/bag/password-change'],
    // Phase 3
    ['42_bag_orders', '/bag/bag-orders'],
    ['43_bag_order_create', '/bag/bag-orders/create'],
    ['44_bag_receivings', '/bag/bag-receivings'],
    ['45_bag_receiving_create', '/bag/bag-receivings/create'],
    ['46_bag_inventory', '/bag/bag-inventory'],
    // Phase 4
    ['47_shop_orders', '/bag/shop-orders'],
    ['48_shop_order_create', '/bag/shop-orders/create'],
    ['49_bag_sales', '/bag/bag-sales'],
    ['50_bag_sale_create', '/bag/bag-sales/create'],
    ['51_bag_issues', '/bag/bag-issues'],
    ['52_bag_issue_create', '/bag/bag-issues/create'],
    // Phase 5
    ['53_report_sales_ledger', '/bag/reports/sales-ledger'],
    ['54_report_daily_summary', '/bag/reports/daily-summary'],
    ['55_report_period_sales', '/bag/reports/period-sales'],
    ['56_report_supply_demand', '/bag/reports/supply-demand'],
  ];

  for (const [name, url] of pages) {
    await page.goto(`${BASE_URL}${url}`);
    await page.waitForTimeout(800);
    await page.screenshot({ path: path.join(DIR, `${name}.png`), fullPage: true });
    console.log(`  ✓ ${name}`);
  }

  await browser.close();
  console.log(`\n${pages.length}개 스크린샷 완료`);
}

run().catch(err => { console.error('실패:', err.message); process.exit(1); });
