const puppeteer = require('puppeteer-core');

(async () => {
  try {
    // Connect to existing Chrome instance
    const browser = await puppeteer.connect({
      browserURL: 'http://127.0.0.1:18800'
    });
    
    const pages = await browser.pages();
    let page = null;
    
    // Find the Mailchimp signup page
    for (const p of pages) {
      const url = p.url();
      if (url.includes('mailchimp.com/signup')) {
        page = p;
        break;
      }
    }
    
    if (!page) {
      console.log('Could not find Mailchimp signup page');
      await browser.disconnect();
      process.exit(1);
    }
    
    console.log(`Found page: ${page.url()}`);
    
    const email = 'jake@burtonmethod.com';
    const password = 'Mailchimp2026!Secure';
    
    // Use JavaScript to fill the form and dismiss banner
    await page.evaluate((email, password) => {
      // Dismiss cookie banner
      const dismissBtn = document.querySelector('button');
      const buttons = Array.from(document.querySelectorAll('button'));
      const dismissButton = buttons.find(btn => btn.textContent.includes('Dismiss'));
      if (dismissButton) {
        dismissButton.click();
        console.log('Clicked dismiss button');
      }
      
      // Wait a bit for banner to disappear
      setTimeout(() => {
        // Find and fill the form fields
        const inputs = document.querySelectorAll('input[type="text"], input[type="email"]');
        const passwordInput = document.querySelector('input[type="password"]');
        
        // Fill email (first input)
        if (inputs[0]) {
          inputs[0].value = email;
          inputs[0].dispatchEvent(new Event('input', { bubbles: true }));
          inputs[0].dispatchEvent(new Event('change', { bubbles: true }));
          console.log('Filled email field');
        }
        
        // Fill username (second input)
        if (inputs[1]) {
          inputs[1].value = email;
          inputs[1].dispatchEvent(new Event('input', { bubbles: true }));
          inputs[1].dispatchEvent(new Event('change', { bubbles: true }));
          console.log('Filled username field');
        }
        
        // Fill password
        if (passwordInput) {
          passwordInput.value = password;
          passwordInput.dispatchEvent(new Event('input', { bubbles: true }));
          passwordInput.dispatchEvent(new Event('change', { bubbles: true }));
          console.log('Filled password field');
        }
        
        // Click Sign up button
        setTimeout(() => {
          const signupButton = buttons.find(btn => btn.textContent.includes('Sign up'));
          if (signupButton) {
            signupButton.click();
            console.log('Clicked Sign up button');
          }
        }, 500);
      }, 1000);
    }, email, password);
    
    console.log('Form fill script executed');
    
    // Wait for navigation
    await page.waitForNavigation({ waitUntil: 'networkidle0', timeout: 30000 });
    console.log(`Navigated to: ${page.url()}`);
    
    // Take screenshot
    await page.screenshot({ path: '/Users/jakeshore/.clawdbot/workspace/signup_result.png' });
    console.log('Screenshot saved');
    
    await browser.disconnect();
    console.log('Signup completed successfully');
    
  } catch (error) {
    console.error('Error:', error.message);
    process.exit(1);
  }
})();
