#!/usr/bin/env python3
"""
Complete Profile and Analyze Full Flow

This script:
1. Logs in with current credentials
2. Handles profile incomplete modal
3. Completes profile to 100% by filling all required fields
4. Then analyzes the complete Assessments flow
"""
import sys
import time
import re
from pathlib import Path

project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from selenium.webdriver.common.by import By
from pages.login_page import LoginPage
from pages.mandatory_reset_page import MandatoryResetPage
from pages.profile_incomplete_page import ProfileIncompletePage
from pages.profile_editor_page import ProfileEditorPage
from pages.student_nav_page import StudentNavPage
from pages.assessments_page import AssessmentsPage
from pages.domains_page import DomainsPage
from pages.domain_assessment_page import DomainAssessmentPage
from pages.domain_feedback_page import DomainFeedbackPage
from pages.feedback_survey_page import FeedbackSurveyPage
from utils.driver_manager import DriverManager
from config.config import ENVIRONMENT, BASE_URL, TEST_USERNAME, TEST_PASSWORD, TEST_NEW_PASSWORD

def analyze_page_locators(driver, page_name, expected_locators):
    """Analyze a page for expected locators"""
    print(f"\n{'='*80}")
    print(f"Analyzing: {page_name}")
    print(f"{'='*80}")
    
    found = []
    missing = []
    
    for locator_name, locator in expected_locators.items():
        try:
            element = driver.find_element(*locator)
            test_id = element.get_attribute("data-testid")
            found.append((locator_name, test_id))
            print(f"  ✅ {locator_name}: Found (data-testid='{test_id}')")
        except:
            missing.append(locator_name)
            print(f"  ❌ {locator_name}: MISSING")
    
    # Scan for all data-testid attributes
    print(f"\n  📋 All data-testid attributes on page:")
    page_source = driver.page_source
    all_test_ids = re.findall(r'data-testid=["\']([^"\']+)["\']', page_source)
    unique_test_ids = sorted(set(all_test_ids))
    
    for test_id in unique_test_ids:
        print(f"      - {test_id}")
    
    return found, missing, unique_test_ids

def complete_profile_steps(profile_editor):
    """
    Complete all profile steps with test data
    
    Args:
        profile_editor: ProfileEditorPage instance
    """
    print("\n" + "="*80)
    print("COMPLETING PROFILE - FILLING ALL STEPS")
    print("="*80)
    
    # Get initial progress
    initial_progress = profile_editor.get_progress_value()
    print(f"\nInitial Progress: {initial_progress}")
    
    # Step 1: Personal Information
    print("\n[Step 1] Filling Personal Information...")
    try:
        # Check if fields are already filled
        first_name = profile_editor.find_element(profile_editor.FIRST_NAME_INPUT).get_attribute("value")
        if not first_name or first_name.strip() == "":
            profile_editor.fill_personal_information(
                first_name="Test",
                last_name="Student",
                gender="Male",
                dob="01/01/2000",
                roll_number="TEST001",
                nationality="Indian"
            )
        else:
            print("  Fields already filled, skipping...")
        profile_editor.click_save()
        time.sleep(2)
        print(f"  Progress after Step 1: {profile_editor.get_progress_value()}")
    except Exception as e:
        print(f"  ⚠️  Step 1: {e}")
    
    # Step 2: Contact Information
    print("\n[Step 2] Filling Contact Information...")
    try:
        profile_editor.click_next()
        time.sleep(1)
        profile_editor.fill_contact_information(
            email="test@example.com",
            phone="9876543210",
            address="123 Test Street",
            city="Bangalore",
            state="Karnataka",
            zip_code="560001",
            native_state="Karnataka"
        )
        profile_editor.click_save()
        time.sleep(2)
        print(f"  Progress after Step 2: {profile_editor.get_progress_value()}")
    except Exception as e:
        print(f"  ⚠️  Step 2: {e}")
    
    # Step 3: Parent/Guardian
    print("\n[Step 3] Filling Parent/Guardian Information...")
    try:
        profile_editor.click_next()
        time.sleep(1)
        profile_editor.fill_parent_guardian(
            father_name="Father Name",
            father_age_range="40-50",
            father_occupation="Engineer",
            father_email="father@example.com",
            mother_name="Mother Name",
            mother_age_range="35-45",
            mother_occupation="Teacher",
            mother_email="mother@example.com"
        )
        profile_editor.click_save()
        time.sleep(2)
        print(f"  Progress after Step 3: {profile_editor.get_progress_value()}")
    except Exception as e:
        print(f"  ⚠️  Step 3: {e}")
    
    # Step 4: Education Details
    print("\n[Step 4] Filling Education Details...")
    try:
        profile_editor.click_next()
        time.sleep(1)
        profile_editor.fill_education_details(
            full_name="Test Student",
            current_grade="12th",
            section="A",
            board_stream="CBSE"
        )
        profile_editor.click_save()
        time.sleep(2)
        print(f"  Progress after Step 4: {profile_editor.get_progress_value()}")
    except Exception as e:
        print(f"  ⚠️  Step 4: {e}")
    
    # Step 5: Focus Areas
    print("\n[Step 5] Selecting Focus Areas...")
    try:
        profile_editor.click_next()
        time.sleep(1)
        # Try to select focus areas (may need to adjust based on actual locators)
        profile_editor.select_focus_areas(
            short_term=["ACADEMICS", "FAMILY", "HEALTH"],
            long_term=["ACADEMICS", "FAMILY", "HEALTH"]
        )
        profile_editor.click_save()
        time.sleep(2)
        print(f"  Progress after Step 5: {profile_editor.get_progress_value()}")
    except Exception as e:
        print(f"  ⚠️  Step 5: {e}")
    
    # Step 6: Self-Assessment
    print("\n[Step 6] Filling Self-Assessment...")
    try:
        profile_editor.click_next()
        time.sleep(1)
        profile_editor.select_strengths(["QUICK_LEARNING", "CURIOSITY", "PROBLEM_SOLVING"])
        profile_editor.select_improvements(["COMMUNICATION", "LEADERSHIP", "CRITICAL_THINKING"])
        profile_editor.click_save()
        time.sleep(2)
        print(f"  Progress after Step 6: {profile_editor.get_progress_value()}")
    except Exception as e:
        print(f"  ⚠️  Step 6: {e}")
    
    # Step 7: Hobbies & Clubs
    print("\n[Step 7] Selecting Hobbies & Clubs...")
    try:
        profile_editor.click_next()
        time.sleep(1)
        profile_editor.select_hobbies(["READING", "SPORTS", "GAMING"])
        profile_editor.select_clubs(["SCIENCE", "MATHEMATICS"])
        profile_editor.click_save()
        time.sleep(2)
        print(f"  Progress after Step 7: {profile_editor.get_progress_value()}")
    except Exception as e:
        print(f"  ⚠️  Step 7: {e}")
    
    # Step 8: Achievements
    print("\n[Step 8] Filling Achievements...")
    try:
        profile_editor.click_next()
        time.sleep(1)
        profile_editor.fill_achievements(
            academics="Merit scholarship",
            sports="School cricket team",
            cultural="Drama club participation",
            others="Community service"
        )
        profile_editor.click_save()
        time.sleep(2)
        print(f"  Progress after Step 8: {profile_editor.get_progress_value()}")
    except Exception as e:
        print(f"  ⚠️  Step 8: {e}")
    
    # Step 9: Expectations
    print("\n[Step 9] Selecting Expectations...")
    try:
        profile_editor.click_next()
        time.sleep(1)
        profile_editor.select_expectations(["SELF_UNDERSTANDING", "CAREER_GUIDANCE", "ACADEMIC_SUPPORT"])
        profile_editor.click_save()
        time.sleep(2)
        print(f"  Progress after Step 9: {profile_editor.get_progress_value()}")
    except Exception as e:
        print(f"  ⚠️  Step 9: {e}")
    
    # Final check
    final_progress = profile_editor.get_progress_value()
    print(f"\n{'='*80}")
    print(f"FINAL PROGRESS: {final_progress}")
    print(f"{'='*80}")
    
    if profile_editor.is_profile_complete():
        print("✅ Profile is 100% complete!")
        return True
    else:
        print(f"⚠️  Profile is still incomplete ({final_progress})")
        print("   Some fields may need manual completion")
        return False

def main():
    """Complete profile and analyze full flow"""
    driver = None
    try:
        print("="*80)
        print("COMPLETE PROFILE & FULL FLOW ANALYSIS")
        print("="*80)
        print(f"Environment: {ENVIRONMENT}")
        print(f"Base URL: {BASE_URL}")
        print(f"Credentials: {TEST_USERNAME} / {'*' * len(TEST_PASSWORD)}")
        print("="*80)
        
        # Create visible browser
        driver = DriverManager.get_driver(headless=False)
        
        # Step 1: Login
        print("\n[STEP 1] Logging in...")
        login_page = LoginPage(driver)
        login_page.login()
        time.sleep(2)
        print(f"  Current URL: {driver.current_url}")
        
        # Step 2: Check for Password Reset
        print("\n[STEP 2] Checking for Password Reset...")
        reset_page = MandatoryResetPage(driver)
        if reset_page.is_modal_present():
            print("  ✅ Password reset modal found")
            print("  Automatically handling password reset...")
            print(f"  Current password: {TEST_PASSWORD}")
            print(f"  New password: Admin@123 (standardized)")
            
            # Automatically reset password
            reset_page.reset_password(
                current_password=TEST_PASSWORD,
                new_password=TEST_NEW_PASSWORD,
                confirm_password=TEST_NEW_PASSWORD
            )
            print("  ✅ Password reset completed successfully")
            time.sleep(2)
            
            # Update config password for future use
            # Note: After reset, the new password should be used for future logins
        else:
            print("  ✅ No password reset required")
        
        # Step 3: Handle Profile Incomplete Modal
        print("\n[STEP 3] Checking for Profile Incomplete Modal...")
        profile_incomplete = ProfileIncompletePage(driver)
        if profile_incomplete.is_modal_present():
            progress = profile_incomplete.get_progress_value()
            print(f"  Profile incomplete - Current Progress: {progress}")
            print("  Clicking Complete Profile button...")
            profile_incomplete.click_complete()
            time.sleep(3)
            print(f"  Navigated to: {driver.current_url}")
            
            # Step 4: Complete Profile
            print("\n[STEP 4] Completing Profile...")
            profile_editor = ProfileEditorPage(driver)
            profile_editor.wait_for_page_load()
            
            # Complete all steps
            profile_complete = complete_profile_steps(profile_editor)
            
            if not profile_complete:
                print("\n⚠️  Profile not 100% complete - some fields may need manual attention")
                print("   Please review and complete manually, then press Enter...")
                input()
            
            # Navigate back to dashboard
            print("\n[STEP 5] Navigating back to Dashboard...")
            driver.get(f"{BASE_URL}/student/dashboard")
            time.sleep(3)
            
            # Verify profile modal is gone
            if profile_incomplete.is_modal_present():
                print("  ❌ Profile modal still present - profile may not be 100%")
            else:
                print("  ✅ Profile modal is gone - profile is complete!")
        else:
            print("  ✅ Profile is already complete")
        
        # Step 6: Navigate to Assessments
        print("\n[STEP 6] Navigating to Assessments...")
        student_nav = StudentNavPage(driver)
        try:
            student_nav.click_assessments()
            time.sleep(3)
            print(f"  Current URL: {driver.current_url}")
            
            # Step 7: Analyze Assessments Page
            print("\n[STEP 7] Analyzing Assessments Page...")
            assessments_page = AssessmentsPage(driver)
            assessments_page.wait_for_page_load()
            
            # Get assessment IDs
            assessment_ids = assessments_page.get_assessment_ids()
            print(f"  Found {len(assessment_ids)} assessment(s): {assessment_ids}")
            
            if assessment_ids:
                first_assessment_id = assessment_ids[0]
                print(f"\n  Analyzing first assessment card (ID: {first_assessment_id})...")
                
                # Check assessment card locators
                try:
                    card = assessments_page.get_assessment_card(first_assessment_id)
                    print(f"    ✅ Assessment card found")
                    action = driver.find_element(By.CSS_SELECTOR, f"[data-testid='assessment_card__{first_assessment_id}_action']")
                    print(f"    ✅ Action button found: {action.text}")
                except Exception as e:
                    print(f"    ❌ Error finding assessment card: {e}")
                
                # Step 8: Click Begin Assessment
                print(f"\n[STEP 8] Clicking Begin Assessment for ID: {first_assessment_id}...")
                try:
                    assessments_page.click_begin_assessment(first_assessment_id)
                    time.sleep(3)
                    print(f"  Current URL: {driver.current_url}")
                    
                    # Step 9: Analyze Domains Page
                    print("\n[STEP 9] Analyzing Domains Page...")
                    domains_page = DomainsPage(driver)
                    domains_page.wait_for_page_load()
                    
                    domain_ids = domains_page.get_all_domain_ids()
                    print(f"  Found {len(domain_ids)} domain(s): {domain_ids}")
                    
                    domains_locators = {
                        "Back Button": domains_page.BACK_BUTTON,
                        "Overall Progress": domains_page.OVERALL_PROGRESS_VALUE,
                    }
                    domains_found, domains_missing, domains_all = analyze_page_locators(
                        driver, "Domains Page", domains_locators
                    )
                    
                    # Analyze domain cards
                    if domain_ids:
                        first_domain_id = domain_ids[0]
                        print(f"\n  Analyzing first domain card (ID: {first_domain_id})...")
                        try:
                            domain_card = domains_page.get_domain_card(first_domain_id)
                            print(f"    ✅ Domain card found")
                            domain_action = driver.find_element(
                                By.CSS_SELECTOR, f"[data-testid='domain_card__{first_domain_id}_action']"
                            )
                            print(f"    ✅ Domain action button found: {domain_action.text}")
                            is_locked = domains_page.is_domain_locked(first_domain_id)
                            print(f"    Locked: {is_locked}")
                        except Exception as e:
                            print(f"    ❌ Error finding domain card: {e}")
                    
                except Exception as e:
                    print(f"  ⚠️  Could not proceed to domains: {e}")
            else:
                print("  ⚠️  No assessments found")
                
        except Exception as e:
            print(f"  ❌ Could not navigate to Assessments: {e}")
            print("  This might be because profile is not 100% complete")
        
        print("\n" + "="*80)
        print("ANALYSIS COMPLETE")
        print("="*80)
        print("\nKeeping browser open for 60 seconds for manual inspection...")
        time.sleep(60)
        
    except Exception as e:
        print(f"\n❌ Error: {type(e).__name__}: {str(e)}")
        import traceback
        traceback.print_exc()
        if driver:
            print(f"Current URL: {driver.current_url}")
            print("Keeping browser open for 30 seconds...")
            time.sleep(30)
    finally:
        if driver:
            DriverManager.quit_driver(driver)

if __name__ == "__main__":
    # Ensure we're using local
    import os
    os.environ["ENVIRONMENT"] = "local"
    os.environ["LOCAL_BASE_URL"] = "http://localhost:3983"
    
    # Reload config
    import importlib
    import config.config
    importlib.reload(config.config)
    
    main()

