import pandas as pd
from openpyxl import load_workbook
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import re
import time
import random

def setup_driver(headless=True):
    chrome_options = Options()
    if headless:
        chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--disable-blink-features=AutomationControlled')
    chrome_options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36')
    chrome_options.add_argument('--window-size=1920,1080')
    
    try:
        driver = webdriver.Chrome(options=chrome_options)
        return driver
    except Exception as e:
        print(f"Error setting up Chrome driver: {e}")
        print("Make sure Chrome and chromedriver are installed")
        return None

def parse_price_value(text):
    if not text:
        return None
    # Remove currency symbols and extract numbers
    cleaned = re.sub(r'[^\d,.\-]', '', text)
    # Replace comma with full-stop for decimal
    cleaned = cleaned.replace(',', '.')
    try:
        return float(cleaned)
    except ValueError:
        return None

def scrape_mtg_stocks_values(driver, url):
    cost = None
    expected_value = None
    try:
        print(f"  Loading page...")
        driver.get(url)
        
        time.sleep(random.uniform(5, 10))
        
        cost_element_id = 'cost-value'
        expected_value_element_id = 'ev-value'
    
        cost_element = driver.find_element(By.ID, cost_element_id)
        cost_text = cost_element.text.strip()
        
        expected_value_element = driver.find_element(By.ID, expected_value_element_id)
        expected_value_text = expected_value_element.text.strip()
        
        print(f"    Cost: '{cost_text}'")
        print(f"    Expected Value: '{expected_value_text}'")
        
        cost = parse_price_value(cost_text)
        expected_value = parse_price_value(expected_value_text)
        
        return {
            'expected_value': expected_value,
            'market_value': cost
        }
        
    except Exception as ex:
        print(f"  ✗ Failed:\nEV: {expected_value}\nCost: {cost}")
        return {
            'expected_value': expected_value,
            'market_value': cost
        }
    
def main():
    workbook_name = 'TCG Sole Trader Copy.xlsx'
    sheet_name = 'MTG Set'
    
    print("Loading workbook...")
    wb = load_workbook(workbook_name)
    
    if sheet_name not in wb.sheetnames:
        print(f"Error: Sheet '{sheet_name}' not found")
        return
    
    sheet = wb[sheet_name]
    table_found = False
    start_row = None
    header_row = None
    

    for row in range(2, max(50, sheet.max_row + 1)):
        cell_value = str(sheet.cell(row, 1).value)
        
        for col in range(1, max(10, sheet.max_column + 1)):
            cell_value = str(sheet.cell(row, col).value)
            if 'EV Play BotBox Link' in cell_value:
                header_row = row
                start_row = row + 1
                table_found = True
                break
        if table_found:
            break
    
    if not table_found:
        print("Error: Could not find 'EV Play BotBox Link' column")
        return
    
    print(f"Found table header at row {header_row}")
    print(f"Starting from row {start_row}")
    
    play_ev_link_col = None
    collector_ev_link_col = None
    play_expected_value_col = None
    play_market_value_col = None
    collector_expected_value_col = None
    collector_market_value_col = None
    
    for col in range(1, sheet.max_column + 1):
        header = str(sheet.cell(header_row, col).value).strip()
        if 'EV Play BotBox Link' in header:
            play_ev_link_col = col
        elif 'EV Collector BotBox Link' in header:
            collector_ev_link_col = col
        elif 'Play Booster Expected Market Value BotBox' in header:
            play_expected_value_col = col
        elif 'Play Booster Sealed Market Value BotBox' in header:
            play_market_value_col = col
        elif 'Collector Booster Expected Market Value BotBox' in header:
            collector_expected_value_col = col
        elif 'Collector Booster Sealed Market Value BotBox' in header:
            collector_market_value_col = col
    
    print(f"Columns - Play EV Link: {play_ev_link_col}, Collector EV Link: {collector_ev_link_col}, Play Expected Value: {play_expected_value_col}, Play Market Value: {play_market_value_col}, Collector Expected Value: {collector_expected_value_col}, Collector Market Value: {collector_market_value_col}")
    
    if not all([play_ev_link_col, collector_ev_link_col, play_expected_value_col, play_market_value_col, collector_expected_value_col, collector_market_value_col]):
        print("Error: Could not find all required columns")
        print(f"  EV Play BotBox Link: {'Found' if play_ev_link_col else 'NOT FOUND'}")
        print(f"  EV Collector BotBox Link: {'Found' if collector_ev_link_col else 'NOT FOUND'}")
        print(f"  Play Booster Expected Market Value BotBox: {'Found' if play_expected_value_col else 'NOT FOUND'}")
        print(f"  Play Booster Sealed Market Value BotBox: {'Found' if play_market_value_col else 'NOT FOUND'}")
        print(f"  Collector Booster Expected Market Value BotBox: {'Found' if collector_expected_value_col else 'NOT FOUND'}")
        print(f"  Collector Booster Sealed Market Value BotBox : {'Found' if collector_market_value_col else 'NOT FOUND'}")
        return
    
    print("Setting up browser automation...")
    driver = setup_driver(headless=False)
    if not driver:
        return
    
    try:
        processed_count = 0
        play_updated_count = 0
        collector_updated_count = 0
        play_cleared_count = 0
        collector_cleared_count = 0
        
        for row in range(start_row, sheet.max_row + 1):
            play_ev_link = sheet.cell(row, play_ev_link_col).value
            collector_ev_link = sheet.cell(row, collector_ev_link_col).value
            
            if play_ev_link is None or play_ev_link == '' or collector_ev_link is None or collector_ev_link == '':
                continue

            processed_count += 1
            print(f"\n{'='*80}")
            print(f"Processing row {row}:\n{play_ev_link}\n{collector_ev_link}")
            print(f"{'='*80}")
            
            play_result = scrape_mtg_stocks_values(driver, play_ev_link)
            play_cost = play_result['market_value']
            play_ev = play_result['expected_value']

            sheet.cell(row, play_market_value_col).value = play_cost if play_cost is not None else ''
            sheet.cell(row, play_expected_value_col).value = play_ev if play_ev is not None else ''
            if play_cost is not None or play_ev is not None:
                play_updated_count += 1
                print(f"  ✓ Play updated - Expected: {play_ev}, Market: {play_cost}")
            else:
                play_cleared_count += 1
                print(f"  ✗ Play cleared values - no matching booster type found")
            
            collector_result = scrape_mtg_stocks_values(driver, collector_ev_link)
            collector_cost = collector_result['market_value']
            collector_ev = collector_result['expected_value']
            sheet.cell(row, collector_market_value_col).value = collector_cost if collector_cost is not None else ''
            sheet.cell(row, collector_expected_value_col).value = collector_ev if collector_ev is not None else ''
            if collector_cost is not None or collector_ev is not None:
                collector_updated_count += 1
                print(f"  ✓ Collector updated - Expected: {collector_ev}, Market: {collector_cost}")
            else:
                collector_cleared_count += 1
                print(f"  ✗ Collector cleared values - no matching booster type found")
        
        print(f"\n{'='*80}")
        print(f"Saving workbook...")
        wb.save(workbook_name)
        
        print(f"\nComplete!")
        print(f"Processed: {processed_count} entries")
        print(f"Play updated: {play_updated_count} entries")
        print(f"Collector updated: {collector_updated_count} entries")
        print(f"Play fields cleared: {play_cleared_count} entries (no matching data)")
        print(f"Collector fields cleared: {collector_cleared_count} entries (no matching data)")
        
    finally:
        driver.quit()

if __name__ == "__main__":
    main()