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

async function inspectPage() {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  
  try {
    console.log('페이지 로드 중...');
    await page.goto('http://localhost:3000', { waitUntil: 'networkidle' });
    
    // 스크린샷 저장
    const screenshotDir = path.join(__dirname, 'test_screenshots');
    if (!fs.existsSync(screenshotDir)) {
      fs.mkdirSync(screenshotDir, { recursive: true });
    }
    await page.screenshot({ path: path.join(screenshotDir, 'inspect_login_page.png') });
    
    // 페이지의 HTML 구조 확인
    const html = await page.content();
    fs.writeFileSync(path.join(__dirname, 'page_structure.html'), html);
    
    // 입력 필드 찾기
    console.log('\n입력 필드 확인:');
    const inputs = await page.locator('input').all();
    console.log(`발견된 input 필드: ${inputs.length}개`);
    
    for (let i = 0; i < inputs.length && i < 10; i++) {
      const input = inputs[i];
      const name = await input.getAttribute('name');
      const type = await input.getAttribute('type');
      const id = await input.getAttribute('id');
      const placeholder = await input.getAttribute('placeholder');
      console.log(`  ${i+1}. name="${name}", type="${type}", id="${id}", placeholder="${placeholder}"`);
    }
    
    // 버튼 찾기
    console.log('\n버튼 확인:');
    const buttons = await page.locator('button').all();
    console.log(`발견된 button 요소: ${buttons.length}개`);
    
    for (let i = 0; i < buttons.length && i < 10; i++) {
      const button = buttons[i];
      const text = await button.textContent();
      const type = await button.getAttribute('type');
      console.log(`  ${i+1}. text="${text?.trim()}", type="${type}"`);
    }
    
    // 로그인 관련 요소
    console.log('\n로그인 관련 요소:');
    const loginElements = await page.locator('*:has-text("로그인"), *:has-text("username"), *:has-text("password")').all();
    console.log(`발견된 요소: ${loginElements.length}개`);
    
  } catch (error) {
    console.error('오류:', error.message);
  } finally {
    await browser.close();
  }
}

inspectPage();
