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_registration_report_export():
    driver = None
    try:
        driver = login_to_cms()
        print("✅ Logged in, proceeding to Reports test")

        # Wait for sidebar
        WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "li.ant-menu-item"))
        )
        print("✅ Sidebar loaded")
        time.sleep(1)

        # Open 'Reports' dropdown
        reports_menu = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//span[text()='Reports']/ancestor::li"))
        )
        reports_menu.click()
        print("✅ Opened 'Reports' dropdown")
        time.sleep(1)

        # === 1. Registration Report ===
        registration_report_link = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, '/reports/registration-report')]"))
        )
        registration_report_link.click()
        print("✅ Clicked 'Registration Report'")
        time.sleep(1)

        # Start date input
        date_input = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Start date']"))
        )
        date_input.click()
        print("✅ Clicked date input")
        time.sleep(1)

        # Select 13th
        WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//div[@class='ant-calendar-date' and text()='13']"))
        ).click()
        print("✅ Selected 13th as start")
        time.sleep(1)

        # Select 30th
        WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//div[@class='ant-calendar-date' and text()='30']"))
        ).click()
        print("✅ Selected 30th as end")
        time.sleep(1)

        # Export CSV
        WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Export CSV']]"))
        ).click()
        print("✅ Exported 'Registration Report' CSV")
        time.sleep(2)

        # === 2. Top-Up Usage Report ===
        top_up_link = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, '/reports/top-up')]"))
        )
        top_up_link.click()
        print("✅ Clicked 'Top-Up Usage Report'")
        time.sleep(2)

        WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Export CSV']]"))
        ).click()
        print("✅ Exported 'Top-Up Usage Report' CSV")
        time.sleep(2)

        # === 3. Mobile Usage Report ===
        mobile_report_link = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, '/reports/mobile-report')]"))
        )
        mobile_report_link.click()
        print("✅ Clicked 'Mobile Usage Report'")
        time.sleep(2)

        # Click date input
        mobile_date_input = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Start date']"))
        )
        mobile_date_input.click()
        print("✅ Clicked date input for Mobile Usage")
        time.sleep(1)

        # Click previous month (to go to April)
        prev_month_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CLASS_NAME, "ant-calendar-prev-month-btn"))
        )
        prev_month_btn.click()
        print("✅ Clicked previous month (to April)")
        time.sleep(1)

        # Select April 1
        WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//div[@class='ant-calendar-date' and text()='1']"))
        ).click()
        print("✅ Selected April 1 as start date")
        time.sleep(1)

        # Select ةشغ 1 (second occurrence of 1)
        may_1 = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "(//div[@class='ant-calendar-date' and text()='1'])[2]"))
        )
        may_1.click()
        print("✅ Selected May 1 as end date")


        # Export Mobile Report CSV
        WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Export CSV']]"))
        ).click()
        print("✅ Exported 'Mobile Usage Report' CSV")
        time.sleep(1)

                # === 4. Station Rating Report ===
        station_rating_link = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, '/reports/station-rating')]"))
        )
        station_rating_link.click()
        print("✅ Clicked 'Station Rating Report'")
        time.sleep(2)

        # Export Station Rating CSV
        WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Export CSV']]"))
        ).click()
        print("✅ Exported 'Station Rating Report' CSV")
        time.sleep(1)


        print("⏳ Waiting 10 seconds before closing browser to observe result...")
        time.sleep(10)

    except Exception as e:
        print(f"❌ Test failed: {e}")
    finally:
        if driver:
            driver.quit()




if __name__ == "__main__":
    test_registration_report_export()
