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

(async () => {
  const browser = await chromium.launch();
  const context = await browser.createContext();
  const page = await context.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');
    
    // Wait for page to be ready
    await page.waitForTimeout(3000);
    
    // Take screenshot of initial state
    await page.screenshot({ path: 'initial_state.png' });
    console.log('Initial state screenshot taken');
    
    // Find and click the quote-formula menu
    console.log('3. Looking for 견적수식관리 menu...');
    const allText = await page.content();
    if (allText.includes('견적수식')) {
      console.log('Found 견적수식 in page content');
    }
    
    // Try to find the button by searching for text
    await page.waitForTimeout(1000);
    const menuButton = await page.locator('text=견적수식관리').first();
    await menuButton.click();
    console.log('Menu clicked');
    
    await page.waitForTimeout(3000);
    await page.screenshot({ path: 'after_menu_click.png' });
    
    // Click auto-quote tab
    console.log('4. Looking for 자동 견적 산출 tab...');
    const autoQuoteTab = await page.locator('text=자동 견적 산출').first();
    await autoQuoteTab.click();
    console.log('Auto-quote tab clicked');
    
    await page.waitForTimeout(2000);
    await page.screenshot({ path: 'auto_quote_tab.png' });
    
    // Test Screen product - Input W0 = 3000, H0 = 2500
    console.log('5. Setting Screen product inputs...');
    
    // Click on Screen button
    const screenBtn = await page.locator('button', { hasText: '스크린' }).first();
    await screenBtn.click();
    console.log('Screen button clicked');
    
    await page.waitForTimeout(500);
    
    // Find and fill W0 input
    const inputs = await page.locator('input[type="number"]').all();
    console.log(`Found ${inputs.length} number inputs`);
    
    if (inputs.length >= 2) {
      await inputs[0].fill('3000');
      console.log('W0 set to 3000');
      
      await inputs[1].fill('2500');
      console.log('H0 set to 2500');
    }
    
    await page.waitForTimeout(1000);
    
    // Click calculate button
    console.log('6. Clicking calculate button...');
    const calcBtn = await page.locator('button', { hasText: /산출|계산/ }).first();
    await calcBtn.click();
    console.log('Calculate button clicked');
    
    await page.waitForTimeout(3000);
    await page.screenshot({ path: 'screen_result.png' });
    console.log('Screen result screenshot taken');
    
    // Get the calculation results
    const resultText = await page.content();
    if (resultText.includes('감기샤프트') || resultText.includes('환봉')) {
      console.log('Results appear to contain expected items');
    }
    
    // Now test Iron product - W0 = 4000, H0 = 3000
    console.log('7. Testing Iron product...');
    
    // Click reset button if exists
    const resetBtn = await page.locator('button', { hasText: '초기화' }).first();
    await resetBtn.click();
    console.log('Reset clicked');
    
    await page.waitForTimeout(1000);
    
    // Click Iron button
    const ironBtn = await page.locator('button', { hasText: '철재' }).first();
    await ironBtn.click();
    console.log('Iron button clicked');
    
    await page.waitForTimeout(500);
    
    // Fill iron inputs
    const inputs2 = await page.locator('input[type="number"]').all();
    if (inputs2.length >= 2) {
      await inputs2[0].fill('4000');
      console.log('W0 set to 4000');
      
      await inputs2[1].fill('3000');
      console.log('H0 set to 3000');
    }
    
    await page.waitForTimeout(1000);
    
    // Click calculate
    const calcBtn2 = await page.locator('button', { hasText: /산출|계산/ }).first();
    await calcBtn2.click();
    console.log('Calculate button clicked for Iron');
    
    await page.waitForTimeout(3000);
    await page.screenshot({ path: 'iron_result.png' });
    console.log('Iron result screenshot taken');
    
    console.log('Test completed successfully');
    
  } catch (error) {
    console.error('Error:', error.message);
    try {
      await page.screenshot({ path: 'error_state.png' });
    } catch (e) {
      console.error('Could not take error screenshot');
    }
  } finally {
    await context.close();
    await browser.close();
  }
})();
