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

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  
  try {
    console.log('1. Loading http://localhost:3000...');
    await page.goto('http://localhost:3000', { waitUntil: 'networkidle', timeout: 30000 });
    console.log('2. Page loaded successfully');
    
    await page.waitForTimeout(3000);
    
    console.log('3. Looking for 견적수식관리 menu...');
    try {
      await page.click('text=견적수식관리');
      console.log('Menu clicked successfully');
    } catch (e) {
      console.log('Could not find menu by text, trying alternative...');
      // Try clicking through all buttons
      const buttons = page.locator('button');
      const count = await buttons.count();
      console.log(`Found ${count} buttons`);
      for (let i = 0; i < count; i++) {
        const text = await buttons.nth(i).textContent();
        if (text && text.includes('견적수식')) {
          await buttons.nth(i).click();
          console.log('Menu clicked via loop');
          break;
        }
      }
    }
    
    await page.waitForTimeout(3000);
    await page.screenshot({ path: 'step1_menu.png' });
    console.log('Step 1 screenshot saved');
    
    console.log('4. Looking for 자동 견적 산출 tab...');
    try {
      await page.click('text=자동 견적 산출');
      console.log('Auto-quote tab clicked');
    } catch (e) {
      console.log('Error clicking tab:', e.message);
    }
    
    await page.waitForTimeout(2000);
    await page.screenshot({ path: 'step2_tab.png' });
    
    console.log('5. Setting Screen product values...');
    
    // Click Screen button
    await page.click('button:has-text("스크린")');
    console.log('Screen button clicked');
    
    await page.waitForTimeout(500);
    
    // Find and fill inputs
    const inputs = page.locator('input[type="number"]');
    const count = await inputs.count();
    console.log(`Found ${count} number inputs`);
    
    if (count >= 2) {
      await inputs.nth(0).fill('3000');
      console.log('W0 set to 3000');
      
      await inputs.nth(1).fill('2500');
      console.log('H0 set to 2500');
    }
    
    await page.waitForTimeout(1000);
    await page.screenshot({ path: 'step3_inputs.png' });
    
    console.log('6. Clicking calculate button...');
    const calcButton = page.locator('button:has-text("산출"), button:has-text("계산")').first();
    await calcButton.click();
    console.log('Calculate clicked');
    
    await page.waitForTimeout(3000);
    await page.screenshot({ path: 'step4_screen_result.png' });
    console.log('Screen result screenshot saved');
    
    // Check for results in page
    const pageContent = await page.content();
    if (pageContent.includes('감기샤프트') || pageContent.includes('환봉')) {
      console.log('✓ Results appear to contain expected items');
    }
    
    console.log('7. Testing Iron product...');
    
    // Reset
    await page.click('button:has-text("초기화")');
    await page.waitForTimeout(1000);
    
    // Click Iron
    await page.click('button:has-text("철재")');
    console.log('Iron button clicked');
    
    await page.waitForTimeout(500);
    
    // Set values
    const inputs2 = page.locator('input[type="number"]');
    if (await inputs2.count() >= 2) {
      await inputs2.nth(0).fill('4000');
      await inputs2.nth(1).fill('3000');
      console.log('Iron values set: W0=4000, H0=3000');
    }
    
    await page.waitForTimeout(1000);
    
    // Calculate
    const calcButton2 = page.locator('button:has-text("산출"), button:has-text("계산")').first();
    await calcButton2.click();
    console.log('Iron calculation clicked');
    
    await page.waitForTimeout(3000);
    await page.screenshot({ path: 'step5_iron_result.png' });
    console.log('Iron result screenshot saved');
    
    console.log('\n========== Test Completed ==========');
    console.log('Screenshots saved:');
    console.log('- step1_menu.png');
    console.log('- step2_tab.png');
    console.log('- step3_inputs.png');
    console.log('- step4_screen_result.png');
    console.log('- step5_iron_result.png');
    
  } catch (error) {
    console.error('Error:', error.message);
    try {
      await page.screenshot({ path: 'error.png' });
    } catch (e) {}
  } finally {
    await page.close();
    await browser.close();
  }
})();
