"""
Dashboard Page Object Model for Cognitive Prism (Local)

Uses data-testid locators and student navigation.
"""
from selenium.webdriver.common.by import By
from pages.base_page import BasePage
from config.config import DASHBOARD_URL, BASE_URL, ASSESSMENTS_URL


class DashboardPage(BasePage):
    """Page Object for Dashboard Page"""
    
    # Locators - Using student_nav scope for navigation
    # Navigation handled by StudentNavPage, but keeping for backward compatibility
    ASSESSMENTS_BUTTON = (By.CSS_SELECTOR, "[data-testid='student_nav__assessments_link']")
    
    # Dashboard specific elements (if any)
    WELCOME_MESSAGE = (By.XPATH, "//h1[contains(text(), 'Hi There!')]")
    
    # Assessment card on dashboard (if present)
    # Pattern: assessment_card__{assignmentId}_action
    
    # Profile Completion Modal Locators (using data-testid - scope: profile_incomplete)
    # Handled by ProfileIncompletePage, but keeping for backward compatibility
    PROFILE_MODAL = (By.CSS_SELECTOR, "[data-testid='profile_incomplete__modal']")
    PROFILE_COMPLETION_BUTTON = (By.CSS_SELECTOR, "[data-testid='profile_incomplete__complete_button']")
    PROFILE_COMPLETION_PERCENTAGE = (By.CSS_SELECTOR, "[data-testid='profile_incomplete__progress_value']")
    
    def __init__(self, driver):
        """Initialize Dashboard Page"""
        super().__init__(driver)
        self.url = DASHBOARD_URL
    
    def navigate(self):
        """Navigate to dashboard"""
        self.navigate_to(self.url)
    
    def click_assessments(self):
        """Click Assessments button and navigate to assessments page"""
        self.click_element(self.ASSESSMENTS_BUTTON)
        # Wait for navigation to assessments page
        self.wait.wait_for_url_contains("/assessments")
    
    def is_profile_modal_present(self):
        """
        Check if profile completion modal is present
        
        Returns:
            bool: True if modal is visible
        """
        try:
            # Check if modal is visible using data-testid
            return self.is_element_visible(self.PROFILE_MODAL, timeout=2)
        except:
            return False
    
    def handle_profile_completion_modal(self):
        """
        Handle profile completion modal if present
        
        This method checks for the modal and clicks "Complete Profile Now" if it appears.
        Note: This will navigate to profile editor page.
        """
        if self.is_profile_modal_present():
            # Wait for button to be clickable
            self.wait.wait_for_element_clickable(self.PROFILE_COMPLETION_BUTTON)
            # Click Complete Profile Now button
            self.click_element(self.PROFILE_COMPLETION_BUTTON)
            # Wait for navigation to profile editor page
            self.wait.wait_for_url_contains("/profile-builder")
            return True
        return False
    
    def get_profile_completion_percentage(self):
        """
        Get profile completion percentage
        
        Returns:
            str: Percentage value
        """
        try:
            return self.get_text(self.PROFILE_COMPLETION_PERCENTAGE)
        except:
            return "0%"
    
    def is_profile_complete(self):
        """
        Check if profile is complete (100%)
        
        Returns:
            bool: True if profile is complete
        """
        percentage = self.get_profile_completion_percentage()
        return "100%" in percentage or percentage == "100"
