"""
Test Cases for Assessments Page

Tests the assessments hub page functionality:
- Page load and rendering
- Assessment cards visibility
- Assessment IDs extraction
- Begin/Continue/Resume buttons
- Navigation to domains page
"""
import pytest
from pages.assessments_page import AssessmentsPage
from selenium.webdriver.common.by import By
from config.config import ASSESSMENTS_URL


@pytest.mark.assessment
@pytest.mark.assessments_page
class TestAssessmentsPage:
    """Test cases for Assessments Page"""
    
    def test_assessments_page_loads(self, assessment_ready_student):
        """Test that assessments page loads successfully"""
        assessments_page = assessment_ready_student['assessments_page']
        driver = assessment_ready_student['driver']
        
        # Verify URL
        assert "/assessments" in driver.current_url, \
               f"Should be on assessments page. Current URL: {driver.current_url}"
        
        # Verify page loaded
        assessments_page.wait_for_page_load()
        
        print(f"✅ Assessments page loaded successfully: {driver.current_url}")
    
    def test_assessment_cards_visible(self, assessment_ready_student):
        """Test that assessment cards are visible on the page"""
        assessments_page = assessment_ready_student['assessments_page']
        
        # Get all assessment cards
        cards = assessments_page.get_all_assessment_cards()
        
        assert len(cards) > 0, "At least one assessment card should be visible"
        
        # Verify cards have data-testid attributes
        for card in cards:
            test_id = card.get_attribute("data-testid")
            assert test_id is not None, "Assessment card should have data-testid attribute"
            assert test_id.startswith("assessment_card__"), \
                   f"Assessment card data-testid should start with 'assessment_card__'. Got: {test_id}"
        
        print(f"✅ Found {len(cards)} assessment card(s) with proper data-testid attributes")
    
    def test_assessment_ids_extraction(self, assessment_ready_student):
        """Test that assessment IDs can be extracted correctly"""
        assessments_page = assessment_ready_student['assessments_page']
        
        # Get assessment IDs
        assessment_ids = assessments_page.get_assessment_ids()
        
        assert len(assessment_ids) > 0, "At least one assessment ID should be found"
        
        # Verify IDs are valid (numeric)
        for assessment_id in assessment_ids:
            assert assessment_id.isdigit(), \
                   f"Assessment ID should be numeric. Got: {assessment_id}"
        
        print(f"✅ Extracted {len(assessment_ids)} assessment ID(s): {assessment_ids}")
    
    def test_assessment_card_structure(self, assessment_ready_student):
        """Test that assessment cards have proper structure"""
        assessments_page = assessment_ready_student['assessments_page']
        
        # Get first assessment card
        assessment_ids = assessments_page.get_assessment_ids()
        if not assessment_ids:
            pytest.skip("No assessments available")
        
        first_assessment_id = assessment_ids[0]
        card = assessments_page.get_assessment_card(first_assessment_id)
        
        # Verify card is visible
        assert card.is_displayed(), "Assessment card should be visible"
        
        # Verify action button exists
        action_button = assessments_page.driver.find_element(
            By.CSS_SELECTOR,
            f"[data-testid='assessment_card__{first_assessment_id}_action']"
        )
        assert action_button.is_displayed(), "Action button should be visible"
        
        # Get button text (Begin/Continue/Resume)
        button_text = action_button.text.strip()
        assert len(button_text) > 0, "Action button should have text"
        
        print(f"✅ Assessment card {first_assessment_id} has proper structure")
        print(f"   Action button text: {button_text}")
    
    def test_begin_assessment_navigation(self, assessment_ready_student):
        """Test that clicking Begin Assessment navigates to domains page"""
        assessments_page = assessment_ready_student['assessments_page']
        driver = assessment_ready_student['driver']
        
        # Get first available assessment
        assessment_ids = assessments_page.get_assessment_ids()
        if not assessment_ids:
            pytest.skip("No assessments available")
        
        assessment_id = assessment_ids[0]
        
        # Click Begin Assessment
        assessments_page.click_begin_assessment(assessment_id)
        
        # Verify navigation to domains page
        assert "/assessment/" in driver.current_url, \
               f"Should navigate to assessment page. Current URL: {driver.current_url}"
        assert "/domains" in driver.current_url, \
               f"Should navigate to domains page. Current URL: {driver.current_url}"
        assert assessment_id in driver.current_url, \
               f"URL should contain assessment ID {assessment_id}. Current URL: {driver.current_url}"
        
        print(f"✅ Successfully navigated to domains page for assessment {assessment_id}")
        print(f"   URL: {driver.current_url}")
    
    def test_multiple_assessments_handling(self, assessment_ready_student):
        """Test handling of multiple assessments on the page"""
        assessments_page = assessment_ready_student['assessments_page']
        
        # Get all assessment IDs
        assessment_ids = assessments_page.get_assessment_ids()
        
        if len(assessment_ids) < 2:
            pytest.skip("Less than 2 assessments available - cannot test multiple handling")
        
        # Verify each assessment has a card and action button
        for assessment_id in assessment_ids:
            card = assessments_page.get_assessment_card(assessment_id)
            assert card.is_displayed(), f"Assessment card {assessment_id} should be visible"
            
            action_button = assessments_page.driver.find_element(
                By.CSS_SELECTOR,
                f"[data-testid='assessment_card__{assessment_id}_action']"
            )
            assert action_button.is_displayed(), \
                   f"Action button for assessment {assessment_id} should be visible"
        
        print(f"✅ All {len(assessment_ids)} assessments have proper structure")






