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
from selenium.webdriver.common.keys import Keys
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_top_up():
    driver = None
    try:
        driver = login_to_cms()
        print("Logged in, proceeding to Top-Up test")

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

        # Click the "Top-Up" link in the sidebar
        topup_link = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//a[.//span[text()='Top-Up']]"))
        )
        topup_link.click()
        print("✔️ Clicked on 'Top-Up' in sidebar")
        time.sleep(2)

        # Click View icon for one data
        view_icon = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//i[contains(@class, 'anticon-right-circle-o')]"))
        )
        view_icon.click()
        print("✔️ Clicked on View icon of one data")
        time.sleep(2)

        # Click Update button
        update_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Update']]"))
        )
        update_btn.click()
        print("✔️ Clicked Update button")
        time.sleep(2)

        # Add a value: First clear any existing value, then input 10
        value_input = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "input[placeholder='Value']"))
        )

        # Clear the input field before entering the new value
        value_input.send_keys(Keys.CONTROL + "a")  # Select all text in the field
        value_input.send_keys(Keys.BACKSPACE)  # Delete the selected text
        time.sleep(1)

        # Ensure the field is empty before entering the new value
        assert value_input.get_attribute('value') == "", "The input field was not cleared!"

        value_input.send_keys("10")  # Enter the new value "10"
        print("✔️ Value input set to 10")
        time.sleep(1)

        # Click Submit button
        submit_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Submit']]"))
        )
        submit_btn.click()
        print("✔️ Submit button clicked")
        time.sleep(1)

        # Click Yes in confirmation
        confirm_yes_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'ant-btn-primary') and .//span[text()='Yes']]"))
        )
        confirm_yes_btn.click()
        print("✔️ Confirmed Submit by clicking Yes")
        time.sleep(2)

        # Click another View icon (for a second data entry)
        view_icon = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "(//i[contains(@class, 'anticon-right-circle-o')])[2]"))
        )
        view_icon.click()
        print("✔️ Clicked on View icon of another data")
        time.sleep(2)

        # Click Update for the second entry
        update_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Update']]"))
        )
        update_btn.click()
        print("✔️ Clicked Update button")
        time.sleep(2)

        # Click Cancel for the second entry
        cancel_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Cancel']]"))
        )
        cancel_btn.click()
        print("✔️ Clicked Cancel button")
        time.sleep(2)

        # Confirm Cancel by clicking Yes
        confirm_yes_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'ant-btn-primary') and .//span[text()='Yes']]"))
        )
        confirm_yes_btn.click()
        print("✔️ Confirmed Cancel by clicking Yes")
        time.sleep(2)

        # ------------------- Add New Top-Up Data -------------------

        # Click the Add button
        add_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Add']]"))
        )
        add_btn.click()
        print("✔️ Clicked Add button")
        time.sleep(2)

        # Input Name ("Marjorie")
        name_input = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "//input[@name='name']"))
        )
        name_input.send_keys("Marjorie")
        print("✔️ Entered Name: Marjorie")
        time.sleep(1)

        # Input Value ("500")
        value_input = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "//input[@placeholder='Value']"))
        )
        value_input.send_keys("500")
        print("✔️ Entered Value: 500")
        time.sleep(1)

        # Click Submit to add the data
        submit_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Submit']]"))
        )
        submit_btn.click()
        print("✔️ Clicked Submit button to add data")
        time.sleep(1)

        # Click Yes in confirmation
        confirm_yes_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'ant-btn-primary') and .//span[text()='Yes']]"))
        )
        confirm_yes_btn.click()
        print("✔️ Confirmed Submit by clicking Yes")
        time.sleep(2)

    finally:
        # Close the browser after a brief wait
        if driver:
            print("Test completed. Closing the browser in 10 seconds...")
            time.sleep(10)
            driver.quit()

if __name__ == "__main__":
    test_top_up()
