/**
 * 전체 페이지 스크린샷 캡처
 * 실행: node e2e/helpers/screenshots.js
 */
const { chromium } = require('@playwright/test');
const path = require('path');
const fs = require('fs');

const BASE_URL = 'http://localhost:8045';
const SCREENSHOT_DIR = path.join(__dirname, '../../screenshots');

const ACCOUNTS = {
  admin: { id: 'tester_admin', password: 'test1234!' },
  local: { id: 'tester_local', password: 'test1234!' },
  user:  { id: 'tester_user',  password: 'test1234!' },
};

async function login(page, account) {
  await page.goto(`${BASE_URL}/login`);
  await page.fill('input[name="login_id"]', account.id);
  await page.fill('input[name="password"]', account.password);
  await page.click('button[type="submit"]');
  await page.waitForURL(url => !url.pathname.includes('/login'), { timeout: 30000 });
}

async function screenshot(page, name, url) {
  if (url) await page.goto(url);
  await page.waitForTimeout(1000);
  const filePath = path.join(SCREENSHOT_DIR, `${name}.png`);
  await page.screenshot({ path: filePath, fullPage: true });
  console.log(`  ✓ ${name}`);
  return filePath;
}

async function run() {
  if (!fs.existsSync(SCREENSHOT_DIR)) fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });

  const browser = await chromium.launch();
  const context = await browser.newContext({ viewport: { width: 1440, height: 900 }, locale: 'ko-KR' });
  const page = await context.newPage();

  console.log('=== 공개 페이지 ===');
  await screenshot(page, '01_home', `${BASE_URL}/`);
  await screenshot(page, '02_login', `${BASE_URL}/login`);
  await screenshot(page, '03_register', `${BASE_URL}/register`);

  console.log('\n=== Super Admin — 지자체 선택 ===');
  await login(page, ACCOUNTS.admin);
  await screenshot(page, '04_admin_select_lg');

  // 지자체 선택
  const radio = page.locator('input[name="lg_idx"]').first();
  await radio.check();
  await page.click('button[type="submit"]');
  await page.waitForURL(url => !url.pathname.includes('select-local-government'), { timeout: 30000 });

  console.log('\n=== 관리자 패널 (Super Admin) ===');
  await screenshot(page, '05_admin_dashboard', `${BASE_URL}/admin`);
  await screenshot(page, '06_admin_users', `${BASE_URL}/admin/users`);
  await screenshot(page, '07_admin_users_create', `${BASE_URL}/admin/users/create`);
  await screenshot(page, '08_admin_login_history', `${BASE_URL}/admin/access/login-history`);
  await screenshot(page, '09_admin_approvals', `${BASE_URL}/admin/access/approvals`);
  await screenshot(page, '10_admin_roles', `${BASE_URL}/admin/roles`);
  await screenshot(page, '11_admin_menus', `${BASE_URL}/admin/menus`);
  await screenshot(page, '12_admin_local_governments', `${BASE_URL}/admin/local-governments`);
  await screenshot(page, '13_admin_lg_create', `${BASE_URL}/admin/local-governments/create`);
  await screenshot(page, '14_admin_designated_shops', `${BASE_URL}/admin/designated-shops`);
  await screenshot(page, '15_admin_ds_create', `${BASE_URL}/admin/designated-shops/create`);

  // 로그아웃
  await page.goto(`${BASE_URL}/logout`);
  await page.waitForURL(/\/login/, { timeout: 10000 });

  console.log('\n=== 일반 사용자 — 대시보드 ===');
  await login(page, ACCOUNTS.user);
  await screenshot(page, '16_user_dashboard', `${BASE_URL}/dashboard`);
  await screenshot(page, '17_dashboard_classic', `${BASE_URL}/dashboard/classic-mock`);
  await screenshot(page, '18_dashboard_modern', `${BASE_URL}/dashboard/modern`);
  await screenshot(page, '19_dashboard_dense', `${BASE_URL}/dashboard/dense`);
  await screenshot(page, '20_dashboard_charts', `${BASE_URL}/dashboard/charts`);
  await screenshot(page, '21_bag_inventory', `${BASE_URL}/bag/inventory-inquiry`);
  await screenshot(page, '22_bag_waste_suibal', `${BASE_URL}/bag/waste-suibal-enterprise`);

  await browser.close();
  console.log(`\n스크린샷 ${fs.readdirSync(SCREENSHOT_DIR).length}개 저장 완료 → ${SCREENSHOT_DIR}`);
}

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