"""
Profile Incomplete Modal Page Object Model

Handles profile completion gate modal.
Scope: profile_incomplete
"""
from selenium.webdriver.common.by import By
from pages.base_page import BasePage


class ProfileIncompletePage(BasePage):
    """Page Object for Profile Incomplete Modal"""
    
    # Locators using data-testid (scope: profile_incomplete)
    MODAL = (By.CSS_SELECTOR, "[data-testid='profile_incomplete__modal']")
    PROGRESS_VALUE = (By.CSS_SELECTOR, "[data-testid='profile_incomplete__progress_value']")
    COMPLETE_BUTTON = (By.CSS_SELECTOR, "[data-testid='profile_incomplete__complete_button']")
    
    def __init__(self, driver):
        """Initialize Profile Incomplete Page"""
        super().__init__(driver)
    
    def is_modal_present(self):
        """
        Check if profile incomplete modal is present
        
        Uses data-testid first, then falls back to CSS selector for modal overlay.
        This handles cases where data-testid might not be present in latest codebase.
        
        Returns:
            bool: True if modal is visible
        """
        # Primary: Check by data-testid (if present in codebase) - FAST detection
        # Use quick check (200ms) for speed
        try:
            from utils.smart_wait_optimizer import SmartWaitOptimizer
            if SmartWaitOptimizer.quick_check_modal_present(self.driver, self.MODAL):
                return True
        except:
            pass
        
        # Fallback: Check for modal overlay by CSS pattern
        # Modal overlay: fixed inset-0 bg-black/50 z-[9999]
        try:
            from selenium.webdriver.support.ui import WebDriverWait
            from selenium.webdriver.support import expected_conditions as EC
            from selenium.webdriver.common.by import By
            
            # Check for modal overlay with z-[9999] (profile incomplete modal)
            overlay_selectors = [
                "div.fixed.inset-0.bg-black\\/50.z-\\[9999\\]",  # Exact match
                "div[class*='fixed'][class*='inset-0'][class*='bg-black'][class*='z-[9999]']",  # Partial match
                "div.fixed.inset-0[class*='z-[9999]']",  # Simplified
            ]
            
            for selector in overlay_selectors:
                try:
                    overlay = WebDriverWait(self.driver, 2).until(
                        EC.presence_of_element_located((By.CSS_SELECTOR, selector))
                    )
                    # Check if overlay is visible (opacity > 0)
                    if overlay.is_displayed():
                        # Verify it's the profile incomplete modal by checking for button
                        try:
                            # Check if complete button is present
                            if self.is_element_present(self.COMPLETE_BUTTON, timeout=1):
                                return True
                        except:
                            # If button not found, check for "Complete Your Profile" text
                            page_text = self.driver.page_source.lower()
                            if "complete your profile" in page_text or "profile completion" in page_text:
                                return True
                except:
                    continue
            
            return False
        except:
            return False
    
    def get_progress_value(self):
        """
        Get profile completion progress percentage
        
        Returns:
            str: Progress value (e.g., "65%")
        """
        try:
            return self.get_text(self.PROGRESS_VALUE)
        except:
            return "0%"
    
    def click_complete(self):
        """Click Complete Profile button - navigates to profile editor"""
        self.click_element(self.COMPLETE_BUTTON)
        # Wait for modal to close and navigation to profile editor
        self.wait.wait_for_element_invisible(self.MODAL)
        # Wait for navigation to profile editor
        self.wait.wait_for_url_contains("/profile-builder")

