// This content script is injected into the OpenAI auth page to handle automatic login

// Function to automatically fill in the login form and submit it
function autoFillLoginForm(username, password) {
  console.log('Auto-filling login form for:', username);
  
  // Check if we're on the username/email input page
  const emailInput = document.querySelector('input[name="username"]');
  if (emailInput) {
    console.log('Found email input, filling with:', username);
    // Fill in the email input
    emailInput.value = username;
    // Trigger input event to activate the continue button
    emailInput.dispatchEvent(new Event('input', { bubbles: true }));
    emailInput.dispatchEvent(new Event('change', { bubbles: true }));
    
    // Wait a moment for the UI to update
    setTimeout(() => {
      // Find and click the continue button
      const continueButton = document.querySelector('button[type="submit"]');
      if (continueButton) {
        console.log('Clicking continue button');
        continueButton.click();
        
        // Set up a listener for when the password field appears
        const passwordObserver = new MutationObserver((mutations, observer) => {
          const passwordInput = document.querySelector('input[name="password"]');
          if (passwordInput) {
            console.log('Found password input, filling with password');
            // Stop observing once we found the password field
            observer.disconnect();
            
            // Fill in the password input
            passwordInput.value = password;
            // Trigger input event
            passwordInput.dispatchEvent(new Event('input', { bubbles: true }));
            passwordInput.dispatchEvent(new Event('change', { bubbles: true }));
            
            // Wait a moment for the UI to update
            setTimeout(() => {
              // Find and click the login button
              const loginButton = document.querySelector('button[type="submit"]');
              if (loginButton) {
                console.log('Clicking login button');
                loginButton.click();
              }
            }, 500);
          }
        });
        
        // Start observing the document for changes
        passwordObserver.observe(document.body, {
          childList: true,
          subtree: true
        });
      }
    }, 500);
  }
  
  // Check if we're already on the password input page
  const passwordInput = document.querySelector('input[name="password"]');
  if (passwordInput && !emailInput) {
    console.log('Found password input directly, filling with password');
    // Fill in the password input
    passwordInput.value = password;
    // Trigger input event
    passwordInput.dispatchEvent(new Event('input', { bubbles: true }));
    passwordInput.dispatchEvent(new Event('change', { bubbles: true }));
    
    // Wait a moment for the UI to update
    setTimeout(() => {
      // Find and click the login button
      const loginButton = document.querySelector('button[type="submit"]');
      if (loginButton) {
        console.log('Clicking login button');
        loginButton.click();
      }
    }, 500);
  }
  // Auto-filling login form for username
}

// Listen for messages from the background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.action === 'autoLogin' && message.username && message.password) {
    autoFillLoginForm(message.username, message.password);
    sendResponse({ success: true });
  }
  return true; // Indicates we'll respond asynchronously
});

// Notify the background script that the content script has loaded
chrome.runtime.sendMessage({ action: 'contentScriptReady' });