from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import sys
import os

try:
    from login import login_to_cms
    print("✅ Successfully imported login_to_cms from login.py")
except ImportError as e:
    print(f"❌ Failed to import login_to_cms: {e}")
    sys.exit(1)

def test_promotions():
    driver = None
    try:
        driver = login_to_cms()
        print("Logged in, proceeding to User Management test")

        WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "li.ant-menu-item"))
        )
        print("Sidebar loaded")

        # Click Promotions tab
        promotions_tab = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.XPATH, "//a[span[text()='Promotions']]"))
        )
        driver.execute_script("arguments[0].scrollIntoView(true);", promotions_tab)
        time.sleep(1)
        promotions_tab.click()
        print("✅ Clicked Promotions tab")

        # Search for Title 'Advisory'
        search_input = WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.XPATH, "//input[@placeholder='Search']"))
        )
        search_input.send_keys("Advisory")
        print("Typed 'Advisory' in search")
        time.sleep(2)

        # Click View icon (first occurrence assumed)
        view_icon = WebDriverWait(driver, 20).until(
            EC.element_to_be_clickable((By.XPATH, "//i[contains(@class, 'anticon-right-circle-o')]"))
        )
        view_icon.click()
        print("Clicked View icon")
        time.sleep(2)

        # Click Update icon (same icon class, assuming second one)
        update_button = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.XPATH, "//button[span[text()='Update']]"))
        )
        driver.execute_script("arguments[0].scrollIntoView(true);", update_button)
        time.sleep(1)
        update_button.click()
        print("✅ Clicked Update button")

        # Click the Cancel button
        cancel_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[span[text()='Cancel']]"))
        )
        driver.execute_script("arguments[0].scrollIntoView(true);", cancel_button)
        time.sleep(1)
        cancel_button.click()
        print("✅ Clicked Cancel button")

        # Wait for and click the Yes button in the confirmation dialog
        yes_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'ant-btn-primary') and span[text()='Yes']]"))
        )
        time.sleep(1)
        yes_button.click()
        print("✅ Clicked Yes on confirmation dialog")

        time.sleep(2)

        # Final screenshot inside 'screenshot' folder
        screenshot_path = os.path.join("screenshot", "promotions_test_result.png")
        driver.save_screenshot(screenshot_path)
        print(f"📸 Screenshot saved as {screenshot_path}")

        print("🕒 Waiting 10 seconds before closing the browser...")
        time.sleep(10)
        driver.quit()

    except Exception as e:
        print(f"❌ Test failed: {e}")
        screenshot_path = os.path.join("screenshot", "promotions_test_error.png")
        driver.save_screenshot(screenshot_path)
        print(f"📸 Error screenshot saved as {screenshot_path}")
        raise

    finally:
        driver.quit()
        print("🚪 Browser closed")

if __name__ == "__main__":
    test_promotions()
