import os
import base64
import requests
from datetime import datetime

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import csv
import subprocess

# استيراد الـ Data Loader المشترك
from shared_data_loader import get_shared_data
# استيراد الـ Checkpoint Manager
from checkpoint_manager import CheckpointManager


# =========================================
# SETTINGS
# =========================================
LOG_FILE = "log.csv"
SCREENSHOT_FOLDER = "screenshots"  # Local folder for screenshots
SCRIPT_NAME = "ofac"  # اسم السكريبت للـ checkpoint


# =========================================
# CSV LOG
# =========================================
if not os.path.exists(LOG_FILE):
    with open(LOG_FILE, 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(["Date", "Name", "Database", "Website", "Status"])


def log(name, database, website, status):
    with open(LOG_FILE, 'a', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow([datetime.now().strftime("%d/%m/%Y %H:%M:%S"), name, database, website, status])


# =========================================
# SELENIUM SETUP
# =========================================
options = Options()
options.add_argument("--headless=new")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--disable-software-rasterizer")
options.add_argument("--disable-extensions")
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--single-process")
options.add_argument("--disable-setuid-sandbox")

driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 40)

os.makedirs("screenshots", exist_ok=True)


# =========================================
# NOTE: Screenshots are now saved locally only
# No remote upload needed - viewer.php reads from local folder
# =========================================


# =========================================
# FETCH NAMES (استخدام البيانات المشتركة)
# =========================================
print("[*] Loading shared data...")
data = get_shared_data()

if not data:
    print("[!] No data received!")
    driver.quit()
    exit()

print(f"[*] Total items to process: {len(data)}")


# =========================================
# CHECKPOINT MANAGER
# =========================================
checkpoint = CheckpointManager(SCRIPT_NAME)

# الحصول على آخر index تم معالجته من log.csv
last_processed_index = checkpoint.get_last_processed_index(SCRIPT_NAME, data)

if last_processed_index >= 0:
    print("\n" + "="*60)
    print("✅ Found previous records in log.csv")
    print("="*60)
    print(f"  Already processed: {last_processed_index + 1}/{len(data)} records")
    print(f"  Continuing from record: {last_processed_index + 2}")
    print("="*60 + "\n")
    start_index = last_processed_index + 1
else:
    print("[*] No previous records found - Starting from beginning")
    start_index = 0

print(f"[*] Starting from index: {start_index}")
print(f"[*] Items to process: {len(data) - start_index}/{len(data)}\n")


# =========================================
# MAIN LOOP (OFAC SEARCH)
# =========================================
for index, item in enumerate(data):
    # تخطي السجلات التي تم معالجتها مسبقاً
    if index < start_index:
        continue

    try:
        name = item.get("name", "") if isinstance(item, dict) else item
        database = item.get("database", "MNX") if isinstance(item, dict) else "MNX"

        if not name:
            # حفظ checkpoint حتى للسجلات الفارغة
            checkpoint.update_progress(index, len(data), {"name": "EMPTY", "database": database})
            continue

        print(f"[{index + 1}/{len(data)}] Searching: {name} (from {database})")

        # ================================
        # OPEN OFAC WEBSITE
        # ================================
        driver.get("https://sanctionssearch.ofac.treas.gov/")

        # input box XPath اللي انت محدده
        box = wait.until(
            EC.element_to_be_clickable((
                By.XPATH,
                "/html/body/form/div[3]/div/div/div[3]/div/div/div/div[2]/table/tbody/tr[2]/td[3]/input"
            ))
        )

        box.clear()
        box.send_keys(name)
        box.send_keys(Keys.ENTER)

        # انتظار تحميل النتائج
        wait.until(lambda d: d.execute_script("return document.readyState=='complete'"))
        driver.execute_script("return new Promise(r => setTimeout(r, 3500));")

        # ================================
        # Check for "Your search has not returned any results."
        # ================================
        try:
            # Wait for the element to be present (up to 10 seconds)
            no_results_element = wait.until(
                EC.presence_of_element_located((
                    By.XPATH,
                    "/html/body/form/div[3]/div/div/div[3]/div/div/div/div[4]/div[2]/div[1]/div/span"
                ))
            )

            if "Your search has not returned any results." in no_results_element.text:
                print("[!] Your search has not returned any results.")
                search_status = "Your search has not returned any results."
            else:
                print("[+] Found results")
                search_status = "Found results"
        except:
            # Element not found means there are results
            print("[+] Found results")
            search_status = "Found results"

        # ================================
        # Screenshot with name + client + date + time + OFACS
        # ================================
        safe_name = (
            name.replace("/", "_")
                .replace("\\", "_")
                .replace(" ", "_")
                .replace(":", "_")
        )

        safe_client = (
            database.replace("/", "_")
                    .replace("\\", "_")
                    .replace(":", "_")
                    .replace("*", "_")
        )

        timestamp = datetime.now().strftime("%d-%m-%Y_%H-%M-%S")

        img_path = f"screenshots/{safe_name}_{safe_client}_{timestamp}_OFACS.png"

        result = driver.execute_cdp_cmd("Page.captureScreenshot", {
            "format": "png",
            "captureBeyondViewport": True
        })

        with open(img_path, "wb") as f:
            f.write(base64.b64decode(result["data"]))

        print("[+] Screenshot saved")

        # Screenshot already saved locally - no upload needed
        log(name, database, "OFAC", search_status)

        # حفظ checkpoint بعد كل سجل ناجح
        checkpoint.update_progress(index, len(data), {"name": name, "database": database, "status": search_status})

    except Exception as e:
        print("[X] Error:", e)
        log(name, database, "OFAC", "Failed")

        # حفظ checkpoint حتى عند الفشل
        checkpoint.update_progress(index, len(data), {"name": name, "database": database, "status": "Failed"})


driver.quit()

# مسح الـ checkpoint بعد الانتهاء بنجاح
checkpoint.clear_checkpoint()

print("\n" + "="*60)
print("✅ Finished successfully!")
print("="*60)
