"""
Component Test: Profile Editor Tab Navigation

Tests profile editor tab navigation in isolation.
"""
import pytest
from pages.login_page import LoginPage
from pages.mandatory_reset_page import MandatoryResetPage
from pages.profile_editor_page import ProfileEditorPage
from config.config import TEST_USERNAME, TEST_PASSWORD, TEST_NEW_PASSWORD
from utils.password_tracker import password_tracker


@pytest.mark.component
@pytest.mark.profile
class TestProfileTabsComponent:
    """Component tests for profile editor tab navigation"""
    
    @pytest.fixture(autouse=True)
    def setup(self, driver):
        """Setup: Login and navigate to profile editor"""
        login_page = LoginPage(driver)
        login_page.login(identifier=TEST_USERNAME, password=None)
        
        # Handle password reset if needed
        reset_page = MandatoryResetPage(driver)
        if reset_page.is_modal_present():
            current_password = password_tracker.get_password(TEST_USERNAME, TEST_PASSWORD)
            try:
                reset_page.reset_password(
                    current_password=current_password,
                    new_password=TEST_NEW_PASSWORD,
                    confirm_password=TEST_NEW_PASSWORD,
                    student_cpid=TEST_USERNAME
                )
            except:
                # Try with TEST_NEW_PASSWORD as current
                try:
                    reset_page.reset_password(
                        current_password=TEST_NEW_PASSWORD,
                        new_password=TEST_NEW_PASSWORD,
                        confirm_password=TEST_NEW_PASSWORD,
                        student_cpid=TEST_USERNAME
                    )
                except:
                    pass  # Password might already be reset
        
        # Navigate to profile editor
        profile_page = ProfileEditorPage(driver)
        profile_page.navigate()
        profile_page.wait_for_page_load()
        
        yield profile_page
    
    def test_all_tabs_navigable(self, setup, driver):
        """Test that all 8 tabs can be navigated"""
        profile_page = setup
        
        # Test navigation to each tab (0-7)
        tab_names = [
            "Personal Information",
            "Parent/Guardian Information",
            "Institution Details",
            "Focus Areas",
            "Self-Assessment",
            "Hobbies & Clubs",
            "Achievements",
            "Expectations"
        ]
        
        for i, tab_name in enumerate(tab_names):
            try:
                profile_page.navigate_to_tab(i)
                print(f"✅ Tab {i+1}/8 ({tab_name}) navigated successfully")
            except Exception as e:
                pytest.fail(f"Failed to navigate to tab {i+1} ({tab_name}): {e}")
    
    def test_tab_0_personal_information(self, setup):
        """Test Personal Information tab (Tab 0)"""
        profile_page = setup
        profile_page.navigate_to_tab(0)
        
        # Verify form fields are visible
        assert profile_page.is_element_visible(profile_page.FIRST_NAME_INPUT, timeout=5), \
               "First name input should be visible"
        assert profile_page.is_element_visible(profile_page.LAST_NAME_INPUT, timeout=5), \
               "Last name input should be visible"
        
        print("✅ Personal Information tab loads correctly")
    
    def test_tab_1_parent_guardian(self, setup):
        """Test Parent/Guardian Information tab (Tab 1)"""
        profile_page = setup
        profile_page.navigate_to_tab(1)
        
        # Verify form fields are visible
        assert profile_page.is_element_visible(profile_page.FATHER_FULL_NAME_INPUT, timeout=5), \
               "Father name input should be visible"
        
        print("✅ Parent/Guardian Information tab loads correctly")
    
    def test_tab_2_institution_details(self, setup):
        """Test Institution Details tab (Tab 2)"""
        profile_page = setup
        profile_page.navigate_to_tab(2)
        
        # Verify form fields are visible
        assert profile_page.is_element_visible(profile_page.CURRENT_GRADE_INPUT, timeout=5), \
               "Current grade input should be visible"
        
        print("✅ Institution Details tab loads correctly")
    
    def test_tab_7_expectations_save_button(self, setup):
        """Test Expectations tab (Tab 7) has Save button"""
        profile_page = setup
        profile_page.navigate_to_tab(7)
        
        # Verify Save button is visible (only on last tab)
        assert profile_page.is_element_visible(profile_page.SAVE_BUTTON, timeout=5), \
               "Save button should be visible on last tab"
        
        print("✅ Save button appears on Expectations tab (last tab)")
    
    def test_tab_scrolling(self, setup):
        """Test tab scrolling for tabs 4-7"""
        profile_page = setup
        
        # Navigate to tabs that require scrolling
        for tab_index in [4, 5, 6, 7]:
            try:
                profile_page.navigate_to_tab(tab_index)
                print(f"✅ Tab {tab_index+1} navigated with scrolling")
            except Exception as e:
                pytest.fail(f"Failed to navigate to tab {tab_index+1} with scrolling: {e}")


