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