"""
Domain Feedback Page Object Model

Handles per-domain feedback modal.
Scope: domain_feedback
"""
from selenium.webdriver.common.by import By
from pages.base_page import BasePage


class DomainFeedbackPage(BasePage):
    """Page Object for Domain Feedback Modal"""
    
    # Locators using data-testid (scope: domain_feedback)
    MODAL = (By.CSS_SELECTOR, "[data-testid='domain_feedback__modal']")
    MODAL_CONTENT = (By.CSS_SELECTOR, "[data-testid='domain_feedback__modal__content']")
    QUESTION1 = (By.CSS_SELECTOR, "[data-testid='domain_feedback__question1']")
    QUESTION1_YES = (By.CSS_SELECTOR, "[data-testid='domain_feedback__question1_yes']")
    QUESTION1_NO = (By.CSS_SELECTOR, "[data-testid='domain_feedback__question1_no']")
    QUESTION1_JUSTIFICATION = (By.CSS_SELECTOR, "[data-testid='domain_feedback__question1_justification']")
    QUESTION2 = (By.CSS_SELECTOR, "[data-testid='domain_feedback__question2']")
    QUESTION2_TEXTAREA = (By.CSS_SELECTOR, "[data-testid='domain_feedback__question2_textarea']")
    SUBMIT_BUTTON = (By.CSS_SELECTOR, "[data-testid='domain_feedback__submit_button']")
    
    def __init__(self, driver):
        """Initialize Domain Feedback Page"""
        super().__init__(driver)
    
    def is_modal_present(self):
        """
        Check if domain feedback modal is present
        
        Returns:
            bool: True if modal is visible
        """
        try:
            return self.is_element_visible(self.MODAL, timeout=5)
        except:
            return False
    
    def has_question1_yes_no(self):
        """Check if question 1 has yes/no options"""
        try:
            return (self.is_element_visible(self.QUESTION1_YES, timeout=1) or 
                   self.is_element_visible(self.QUESTION1_NO, timeout=1))
        except:
            return False
    
    def has_question1_reason(self):
        """Check if question 1 has reason textarea"""
        try:
            return self.is_element_visible(self.QUESTION1_TEXTAREA, timeout=1)
        except:
            return False
    
    def has_question2_textarea(self):
        """Check if question 2 textarea exists"""
        try:
            return self.is_element_visible(self.QUESTION2_TEXTAREA, timeout=1)
        except:
            return False
    
    def select_question1_yes(self):
        """Select Yes for question 1"""
        self.click_element(self.QUESTION1_YES)
    
    def enter_question1_reason(self, text):
        """Enter reason for question 1"""
        self.send_keys(self.QUESTION1_TEXTAREA, text)
    
    def enter_question2_text(self, text):
        """Enter text for question 2"""
        self.send_keys(self.QUESTION2_TEXTAREA, text)
    
    def click_submit(self):
        """Click submit button"""
        self.click_element(self.SUBMIT_BUTTON)
    
    def answer_question1(self, yes=True):
        """
        Answer question 1 (yes/no)
        
        Args:
            yes: True for yes, False for no
        """
        if yes:
            self.click_element(self.QUESTION1_YES)
        else:
            self.click_element(self.QUESTION1_NO)
    
    def fill_question1_justification(self, text):
        """Fill question 1 justification textarea (appears when No is selected)"""
        self.send_keys(self.QUESTION1_JUSTIFICATION, text)
    
    def fill_question1_textarea(self, text):
        """Alias for fill_question1_justification"""
        self.fill_question1_justification(text)
    
    def fill_question2_textarea(self, text):
        """Fill question 2 textarea"""
        self.send_keys(self.QUESTION2_TEXTAREA, text)
    
    def submit_feedback(self, question1_yes=True, question1_justification="", question2_text=""):
        """
        Complete and submit domain feedback
        
        Args:
            question1_yes: Answer to question 1 (True for Yes, False for No)
            question1_justification: Justification text for question 1 (required if question1_yes=False)
            question2_text: Text for question 2
        """
        if not self.is_modal_present():
            raise Exception("Domain feedback modal is not present")
        
        # Answer question 1
        self.answer_question1(question1_yes)
        
        # Fill justification if "No" was selected
        if not question1_yes and question1_justification:
            # Wait for justification textarea to appear
            self.wait.wait_for_element_visible(self.QUESTION1_JUSTIFICATION, timeout=3)
            self.send_keys(self.QUESTION1_JUSTIFICATION, question1_justification)
        
        # Fill question 2
        if question2_text:
            self.send_keys(self.QUESTION2_TEXTAREA, question2_text)
        
        # Submit
        self.click_element(self.SUBMIT_BUTTON)
        # Wait for modal to close and redirect to domains page
        self.wait.wait_for_element_invisible(self.MODAL, timeout=10)
        self.wait.wait_for_url_contains("/domains")

