import { chromium } from "@playwright/test";

(async () => {
  const b = await chromium.launch();
  const p = await b.newPage();
  
  // Capture console logs
  p.on("console", msg => console.log(`[console] ${msg.type()}: ${msg.text()}`));
  
  try {
    await p.goto("http://localhost:5265/login");
    console.log("1. Login page loaded");
    
    // Try to fill form
    const userInput = await p.$("input[name=\"username\"]");
    if (!userInput) {
      console.log("✗ Username input not found!");
      const content = await p.content();
      if (content.includes("관리자 아이디")) {
        console.log("  → But 'Blazor login form' text found (Blazor component)");
      }
    } else {
      await p.fill("input[name=\"username\"]", "admin");
      await p.fill("input[name=\"password\"]", "admin");
      console.log("2. Form filled");
      
      // Submit
      await p.click("button[type=\"submit\"]");
      console.log("3. Button clicked");
      
      // Wait and check
      await new Promise(r => setTimeout(r, 5000));
      
      const finalUrl = p.url();
      const finalContent = await p.content();
      
      console.log(`4. After 5 seconds:`);
      console.log(`   URL: ${finalUrl}`);
      
      if (finalContent.includes("로그인 실패")) {
        console.log("   ✗ Login failed error shown");
      } else if (finalContent.includes("오류")) {
        console.log("   ✗ Error shown");
      } else if (finalContent.includes("로그인 성공")) {
        console.log("   ✓ Login success message shown");
      }
    }
    
  } catch (e) {
    console.error("Error:", e.message);
  }
  
  await b.close();
})();
