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

(async () => {
  const verificationCode = '257233';
  const email = 'jake@burtonmethod.com';
  
  // Use the existing Chrome instance user data directory
  const browser = await chromium.connectOverCDP('http://127.0.0.1:18800');
  const context = browser.contexts()[0];
  const page = await context.newPage();
  
  try {
    // Navigate to HubSpot verification page with token
    await page.goto('https://app.hubspot.com/signup-hubspot/crm?token=ef56a6e66e4cade8d4e070ff84df3611aaf12410e3a0500b31611a99e7db632bd8146ccb608a6db2be9c634c01fbdd50&signupMode=standalone&flowPath=crm&step=code_verification', {
      waitUntil: 'networkidle',
      timeout: 30000
    });
    
    console.log('Page loaded');
    
    // Wait for page to load and check what's visible
    await page.waitForTimeout(2000);
    
    // Take a screenshot to see what we have
    await page.screenshot({ path: '/Users/jakeshore/.clawdbot/workspace/hubspot-1.png' });
    console.log('Screenshot saved to hubspot-1.png');
    
    // Try to find and enter the verification code
    // Look for input field that might contain the code
    const codeInput = await page.locator('input[type="text"], input[type="number"], input[name*="code"], input[id*="code"]').first();
    
    if (await codeInput.isVisible({ timeout: 5000 })) {
      await codeInput.fill(verificationCode);
      console.log('Entered verification code');
      
      // Look for submit button
      const submitButton = await page.locator('button:has-text("Verify"), button:has-text("Submit"), button:has-text("Continue")').first();
      await submitButton.click();
      console.log('Clicked submit button');
      
      // Wait for navigation
      await page.waitForTimeout(5000);
      await page.screenshot({ path: '/Users/jakeshore/.clawdbot/workspace/hubspot-2.png' });
      console.log('Screenshot after submit saved to hubspot-2.png');
    }
    
    console.log('Current URL:', page.url());
    
  } catch (error) {
    console.error('Error:', error.message);
    await page.screenshot({ path: '/Users/jakeshore/.clawdbot/workspace/hubspot-error.png' });
  }
  
  // Keep page open for manual interaction if needed
  console.log('Browser page remains open for further navigation');
  
})();
