"""
Quick script to check what profile editor tabs are actually present in the DOM
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from utils.driver_manager import DriverManager
from pages.login_page import LoginPage
from pages.mandatory_reset_page import MandatoryResetPage
from pages.profile_incomplete_page import ProfileIncompletePage
from pages.profile_editor_page import ProfileEditorPage
from config.config import TEST_USERNAME, TEST_PASSWORD, TEST_NEW_PASSWORD
from selenium.webdriver.common.by import By
import time

def get_all_tabs_from_dom(driver):
    """Get all tab elements from DOM"""
    # Try multiple selectors
    selectors = [
        "[data-testid^='profile_editor__tab_']",
        "button[data-testid^='profile_editor__tab_']",
        "[data-testid*='tab']",
    ]
    
    all_tabs = []
    for selector in selectors:
        try:
            tabs = driver.find_elements(By.CSS_SELECTOR, selector)
            if tabs:
                for tab in tabs:
                    testid = tab.get_attribute('data-testid')
                    text = tab.text
                    is_displayed = tab.is_displayed()
                    all_tabs.append({
                        'testid': testid,
                        'text': text,
                        'displayed': is_displayed,
                        'selector': selector
                    })
        except:
            pass
    
    return all_tabs

def main():
    print("🌐 Checking Profile Editor Tabs in DOM...")
    
    driver = DriverManager.get_driver()
    try:
        # Login
        login_page = LoginPage(driver)
        login_page.login(identifier=TEST_USERNAME, password=None)
        
        # Handle password reset
        reset_page = MandatoryResetPage(driver)
        if reset_page.is_modal_present():
            from utils.password_tracker import password_tracker
            current_password = password_tracker.get_password(TEST_USERNAME, TEST_PASSWORD)
            reset_page.reset_password(
                current_password=current_password,
                new_password=TEST_NEW_PASSWORD,
                confirm_password=TEST_NEW_PASSWORD,
                student_cpid=TEST_USERNAME
            )
        
        # Handle profile incomplete
        profile_incomplete = ProfileIncompletePage(driver)
        if profile_incomplete.is_modal_present():
            profile_incomplete.click_complete()
            time.sleep(2)
        
        # Navigate to profile editor
        profile_editor = ProfileEditorPage(driver)
        profile_editor.navigate()
        profile_editor.wait_for_page_load()
        time.sleep(3)  # Wait for tabs to render
        
        print("\n📋 Checking for tabs in DOM...")
        tabs = get_all_tabs_from_dom(driver)
        
        if tabs:
            print(f"\n✅ Found {len(tabs)} tab(s):")
            for i, tab in enumerate(tabs, 1):
                print(f"  {i}. testid: {tab['testid']}")
                print(f"     text: {tab['text']}")
                print(f"     displayed: {tab['displayed']}")
                print()
        else:
            print("\n❌ No tabs found with data-testid attributes")
            print("\n🔍 Checking for any buttons that might be tabs...")
            all_buttons = driver.find_elements(By.TAG_NAME, "button")
            print(f"Found {len(all_buttons)} buttons total")
            for i, btn in enumerate(all_buttons[:20], 1):  # First 20 buttons
                if btn.is_displayed():
                    text = btn.text.strip()
                    testid = btn.get_attribute('data-testid')
                    if text or testid:
                        print(f"  {i}. text: '{text}', testid: '{testid}'")
        
        # Also check page source for tab-related data-testids
        print("\n🔍 Searching page source for 'profile_editor__tab'...")
        page_source = driver.page_source
        import re
        tab_testids = re.findall(r'data-testid="(profile_editor__tab_[^"]+)"', page_source)
        if tab_testids:
            print(f"✅ Found {len(set(tab_testids))} unique tab testids in page source:")
            for testid in sorted(set(tab_testids)):
                print(f"  - {testid}")
        else:
            print("❌ No tab testids found in page source")
        
    finally:
        DriverManager.quit_driver(driver)

if __name__ == "__main__":
    main()


