"""
Complete Student Flow Test Cases

Tests the complete student journey in correct sequence:
1. Login
2. Password Reset (if needed)
3. Profile Completion (if needed)
4. Logout

This ensures password reset happens before logout, preventing blockers.
"""
import pytest
from pages.login_page import LoginPage
from pages.mandatory_reset_page import MandatoryResetPage
from pages.profile_incomplete_page import ProfileIncompletePage
from pages.dashboard_page import DashboardPage
from config.config import TEST_USERNAME, TEST_PASSWORD, TEST_NEW_PASSWORD


@pytest.mark.authentication
@pytest.mark.integration
class TestCompleteStudentFlow:
    """Test cases for complete student flow in correct sequence"""
    
    def test_complete_flow_with_password_reset(self, driver):
        """
        Complete flow: Login → Password Reset → Dashboard → Logout
        
        This test ensures password reset happens first, preventing
        mandatory reset popup from blocking logout.
        """
        # Step 1: Login (use tracked password if available)
        from utils.password_tracker import password_tracker
        password = password_tracker.get_password(TEST_USERNAME, TEST_PASSWORD)
        login_page = LoginPage(driver)
        login_page.login(identifier=TEST_USERNAME, password=password)
        
        # Step 2: Handle Password Reset (if needed)
        reset_page = MandatoryResetPage(driver)
        if reset_page.is_modal_present():
            print("✅ Password reset modal detected - resetting password")
            # CRITICAL: Get current password from tracker (not hardcoded TEST_PASSWORD)
            current_password = password_tracker.get_password(TEST_USERNAME, TEST_PASSWORD)
            print(f"🔐 Using current password from tracker: {current_password[:3]}...")
            
            # CRITICAL: Always reset to TEST_NEW_PASSWORD (not TEST_PASSWORD)
            reset_page.reset_password(
                current_password=current_password,
                new_password=TEST_NEW_PASSWORD,  # ✅ Always use TEST_NEW_PASSWORD
                confirm_password=TEST_NEW_PASSWORD,
                student_cpid=TEST_USERNAME
            )
            print("✅ Password reset completed")
            # Password tracker is updated automatically by reset_password method
        else:
            print("✅ Password already reset - proceeding")
        
        # Step 3: Verify on Dashboard (or profile builder if modal was handled)
        dashboard_page = DashboardPage(driver)
        current_url = driver.current_url
        assert "/dashboard" in current_url or "/profile-builder" in current_url or "/student" in current_url, \
               f"Should be on dashboard or profile builder after login/password reset. Current URL: {current_url}"
        print("✅ Successfully on dashboard/profile builder")
        
        # Step 4: Handle Profile Incomplete Modal (if present)
        profile_incomplete = ProfileIncompletePage(driver)
        if profile_incomplete.is_modal_present():
            print("⚠️  Profile incomplete modal present - skipping logout test")
            print("   (Profile must be completed before logout can be tested)")
            pytest.skip("Profile incomplete - cannot test logout")
        
        # Step 5: Logout (only if profile is complete)
        # Note: Logout functionality needs to be implemented
        # For now, we just verify we're on dashboard
        print("✅ Complete flow test passed")
    
    def test_login_password_reset_sequence(self, driver):
        """
        Test that password reset happens before any other operations
        
        This ensures the mandatory reset popup doesn't block other tests.
        """
        # Login (use tracked password if available)
        from utils.password_tracker import password_tracker
        password = password_tracker.get_password(TEST_USERNAME, TEST_PASSWORD)
        login_page = LoginPage(driver)
        login_page.login(identifier=TEST_USERNAME, password=password)
        
        # Check for password reset immediately
        reset_page = MandatoryResetPage(driver)
        password_reset_required = reset_page.is_modal_present()
        
        if password_reset_required:
            # Reset password first (with CPID for tracking)
            # CRITICAL: Get current password from tracker (not hardcoded TEST_PASSWORD)
            current_password = password_tracker.get_password(TEST_USERNAME, TEST_PASSWORD)
            print(f"🔐 Using current password from tracker: {current_password[:3]}...")
            
            # CRITICAL: Always reset to TEST_NEW_PASSWORD (not TEST_PASSWORD)
            reset_page.reset_password(
                current_password=current_password,
                new_password=TEST_NEW_PASSWORD,  # ✅ Always use TEST_NEW_PASSWORD
                confirm_password=TEST_NEW_PASSWORD,
                student_cpid=TEST_USERNAME
            )
            print("✅ Password reset completed - student can now proceed")
        else:
            print("✅ Password already reset - student can proceed")
        
        # Verify we can proceed (not blocked by reset modal)
        assert not reset_page.is_modal_present(), \
               "Password reset modal should be closed after reset"
        
        # Verify on dashboard
        assert "/dashboard" in driver.current_url or "/student" in driver.current_url, \
               "Should be on dashboard after password reset"
        
        print("✅ Password reset sequence test passed")

