"""
Feedback Survey Page Object Model

Handles overall test feedback modal.
Scope: feedback_survey
"""
from selenium.webdriver.common.by import By
from pages.base_page import BasePage


class FeedbackSurveyPage(BasePage):
    """Page Object for Feedback Survey Modal"""
    
    # Locators using data-testid (scope: feedback_survey)
    OVERALL_MODAL = (By.CSS_SELECTOR, "[data-testid='feedback_survey__overall_modal']")
    PER_QUESTION_MODAL = (By.CSS_SELECTOR, "[data-testid='feedback_survey__per_question_modal']")
    SUBMIT_BUTTON = (By.CSS_SELECTOR, "[data-testid='feedback_survey__submit_button']")
    
    def __init__(self, driver):
        """Initialize Feedback Survey Page"""
        super().__init__(driver)
    
    def is_overall_modal_present(self):
        """Check if overall feedback modal is present"""
        try:
            return self.is_element_visible(self.OVERALL_MODAL, timeout=5)
        except:
            return False
    
    def is_per_question_modal_present(self):
        """Check if per-question feedback modal is present"""
        try:
            return self.is_element_visible(self.PER_QUESTION_MODAL, timeout=5)
        except:
            return False
    
    def set_overall_rating(self, rating):
        """
        Set overall rating (1-5)
        
        Args:
            rating: Rating value (1-5)
        """
        rating_locator = (By.CSS_SELECTOR, f"[data-testid='feedback_survey__overall_rating_{rating}']")
        self.click_element(rating_locator)
    
    def set_question_rating(self, question_id, rating):
        """
        Set rating for a specific question
        
        Args:
            question_id: Question ID
            rating: Rating value (1-5)
        """
        rating_locator = (By.CSS_SELECTOR, f"[data-testid='feedback_survey__question_{question_id}_rating_{rating}']")
        self.click_element(rating_locator)
    
    def set_question_comment(self, question_id, comment):
        """
        Set comment for a specific question
        
        Args:
            question_id: Question ID
            comment: Comment text
        """
        comment_locator = (By.CSS_SELECTOR, f"[data-testid='feedback_survey__question_{question_id}_comment']")
        self.send_keys(comment_locator, comment)
    
    def submit_feedback(self):
        """Submit feedback survey"""
        self.click_element(self.SUBMIT_BUTTON)
        # Wait for modal to close
        self.wait.wait_for_element_invisible(self.OVERALL_MODAL)
        self.wait.wait_for_element_invisible(self.PER_QUESTION_MODAL)
    
    def is_final_feedback_available(self):
        """Check if any final feedback modal is available"""
        return self.is_overall_modal_present() or self.is_per_question_modal_present()
    
    def get_feedback_questions(self):
        """
        Get all feedback question IDs
        
        Returns:
            list: List of question IDs
        """
        import re
        questions = self.driver.find_elements(By.CSS_SELECTOR, "[data-testid^='feedback_survey__question_']")
        question_ids = set()
        for question in questions:
            test_id = question.get_attribute("data-testid")
            if test_id:
                match = re.search(r'question_(\d+)_', test_id)
                if match:
                    question_ids.add(match.group(1))
        return list(question_ids)
    
    def select_overall_rating(self, rating):
        """Alias for set_overall_rating"""
        self.set_overall_rating(rating)
    
    def select_question_rating(self, question_id, rating):
        """Alias for set_question_rating"""
        self.set_question_rating(question_id, rating)
    
    def enter_question_comment(self, question_id, comment):
        """Alias for set_question_comment"""
        self.set_question_comment(question_id, comment)

