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 = "sam"  # اسم السكريبت للـ 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
# =========================================
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
# =========================================
for index, item in enumerate(data):
    # تخطي السجلات التي تم معالجتها مسبقاً
    if index < start_index:
        continue

    try:
        # Handle both old format (string) and new format (object)
        if isinstance(item, dict):
            name = item.get("name", "")
            database = item.get("database", "Unknown")
        else:
            # Old format - just a string
            name = item
            database = "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})")

        driver.get("https://sam.gov/search")

        box = wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "app-keyword-text 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, 5000));")

        # =================================
        # Check for "No matches found"
        # =================================
        search_status = "Found results"  # Default

        try:
            # Try to find "No matches found" element
            no_matches_elements = driver.find_elements(
                By.XPATH,
                "//*[contains(text(), 'No matches found')]"
            )

            if no_matches_elements and len(no_matches_elements) > 0:
                print("[!] No matches found")
                search_status = "No matches found"
            else:
                print("[+] Found results")
                search_status = "Found results"
        except Exception as e:
            print(f"[!] Error checking results: {e}")
            print("[+] Assuming found results")
            search_status = "Found results"

        # =================================
        # Screenshot with name + client + date + time
        # =================================
        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}_SAM.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, "SAM", 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, "SAM", "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)
