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):
    """Setup Chrome driver"""
    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):
    """Extract numeric value from price string like '$5.50' or '€5,50'"""
    if not text:
        return None
    # Remove currency symbols and extract numbers
    cleaned = re.sub(r'[^\d,.\-]', '', text)
    # Replace comma with period for decimal
    cleaned = cleaned.replace(',', '.')
    try:
        return float(cleaned)
    except ValueError:
        return None

def scrape_mtg_stocks_values(driver, url):
    """Scrape expected value and market value from MTG Stocks"""
    try:
        print(f"  Loading page...")
        driver.get(url)
        
        # Wait for table to load
        time.sleep(random.uniform(10, 20))
        
        # Valid booster types to match
        valid_play_booster_types = [
            'Play Booster Pack',
            'Set Booster Pack',
            'Booster Pack',
            'Play Booster',
            'Set Booster',
            'Booster'
        ]
        valid_collector_booster_types = [
            'Collector Booster Pack',
            'Collector Booster'
        ]
        
        # Find all rows in the table
        row_selector = 'mtg-sets-expected-value > mtg-product-tree > .table-responsive > table > tbody:nth-child(2) > tr'
        rows = driver.find_elements(By.CSS_SELECTOR, row_selector)
        
        print(f"  Found {len(rows)} rows in table")
        found_play = False
        found_collector = False
        play_expected_value = None
        play_market_value = None
        collector_expected_value = None
        collector_market_value = None

        for row in rows:
            try:
                # Get the booster type from first column
                booster_type_elem = row.find_element(By.CSS_SELECTOR, 'td:nth-child(1) > div.d-flex.align-items-center:nth-child(1) > a:nth-child(2)')
                booster_type = booster_type_elem.text.strip()
                
                print(f"    Checking row: '{booster_type}'")
                
                if booster_type in valid_play_booster_types and found_play == False:
                    print(f"    ✓ Match found: '{booster_type}'")
                    found_play = True

                    # Get expected value (3rd column)
                    expected_value_elem = row.find_element(By.CSS_SELECTOR, 'td:nth-child(3)')
                    expected_value_text = expected_value_elem.text.strip()
                    
                    # Get market value (5th column)
                    market_value_elem = row.find_element(By.CSS_SELECTOR, 'td:nth-child(5)')
                    market_value_text = market_value_elem.text.strip()
                    
                    print(f"    Expected Value: '{expected_value_text}'")
                    print(f"    Market Value: '{market_value_text}'")
                    
                    # Parse values
                    play_expected_value = parse_price_value(expected_value_text)
                    play_market_value = parse_price_value(market_value_text)
                
                if booster_type in valid_collector_booster_types and found_collector == False:
                    print(f"    ✓ Match found: '{booster_type}'")
                    found_collector = True
                    
                    # Get expected value (3rd column)
                    expected_value_elem = row.find_element(By.CSS_SELECTOR, 'td:nth-child(3)')
                    expected_value_text = expected_value_elem.text.strip()
                    
                    # Get market value (5th column)
                    market_value_elem = row.find_element(By.CSS_SELECTOR, 'td:nth-child(5)')
                    market_value_text = market_value_elem.text.strip()
                    
                    print(f"    Expected Value: '{expected_value_text}'")
                    print(f"    Market Value: '{market_value_text}'")
                    
                    # Parse values
                    collector_expected_value = parse_price_value(expected_value_text)
                    collector_market_value = parse_price_value(market_value_text)
                    
                if found_play and found_collector:
                    return {
                        'play_expected_value': play_expected_value,
                        'play_market_value': play_market_value,
                        'collector_expected_value': collector_expected_value,
                        'collector_market_value': collector_market_value,
                        'found_play': True,
                        'found_collector': True
                    }
                
                    
            except Exception as e:
                # Row doesn't match structure, continue to next
                continue
        
        print(f"  ✗ No matching booster type found")
        return {
            'play_expected_value': play_expected_value,
            'play_market_value': play_market_value,
            'collector_expected_value': collector_expected_value,
            'collector_market_value': collector_market_value,
            'found_play': found_play,
            'found_collector': found_collector
        }
        
    except Exception as e:
        print(f"  Error: {e}")
        return {
            'play_expected_value': play_expected_value,
            'play_market_value': play_market_value,
            'collector_expected_value': collector_expected_value,
            'collector_market_value': collector_market_value,
            'found_play': found_play,
            'found_collector': found_collector
        }

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)
        # Check multiple columns for table indicators
        for col in range(1, max(10, sheet.max_column + 1)):
            cell_value = str(sheet.cell(row, col).value)
            if 'EV MTG Stocks 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 MTG Stocks Link' column")
        return
    
    print(f"Found table header at row {header_row}")
    print(f"Starting from row {start_row}")
    
    # Find column indices
    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 MTG Stocks Link' in header:
            ev_link_col = col
        elif 'Play Booster Expected Market Value MTG Stocks' in header:
            play_expected_value_col = col
        elif 'Play Booster Sealed Market Value MTG Stocks' in header:
            play_market_value_col = col
        elif 'Collector Booster Expected Market Value MTG Stocks' in header:
            collector_expected_value_col = col
        elif 'Collector Booster Sealed Market Value MTG Stocks' in header:
            collector_market_value_col = col
    
    print(f"Columns - EV Link: {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([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 MTG Stocks Link: {'Found' if ev_link_col else 'NOT FOUND'}")
        print(f"  Play Booster Expected Market Value: {'Found' if play_expected_value_col else 'NOT FOUND'}")
        print(f"  Play Booster Sealed Market Value: {'Found' if play_market_value_col else 'NOT FOUND'}")
        print(f"  Collector Booster Expected Market Value: {'Found' if collector_expected_value_col else 'NOT FOUND'}")
        print(f"  Collector Booster Sealed Market Value: {'Found' if collector_market_value_col else 'NOT FOUND'}")
        return
    
    print("Setting up browser automation...")
    driver = setup_driver(headless=False)
    if not driver:
        return
    
    print(f'Sheet dimensions: {sheet.max_row} rows x {sheet.max_column} columns')

    try:
        processed_count = 0
        updated_count = 0
        play_cleared_count = 0
        collector_cleared_count = 0
        
        for row in range(start_row, sheet.max_row + 1):
            ev_link = sheet.cell(row, ev_link_col).value
            processed_count += 1
            print(f"\n{'='*80}")
            print(f"Processing row {row}: {ev_link}")
            print(f"{'='*80}")
            
            result = scrape_mtg_stocks_values(driver, ev_link)
            
            if result['found_play']:
                # Update cells with found values
                sheet.cell(row, play_expected_value_col).value = result['play_expected_value']
                sheet.cell(row, play_market_value_col).value = result['play_market_value']
                updated_count += 1
                print(f"  ✓ Updated - Expected: {result['play_expected_value']}, Market: {result['play_market_value']}")
            else:
                # Clear cells - no matching booster type found
                sheet.cell(row, play_expected_value_col).value = ''
                sheet.cell(row, play_market_value_col).value = ''
                play_cleared_count += 1
                print(f"  ✗ Cleared values - no matching booster type found")
            
            if result['found_collector']:
                # Update cells with found values
                sheet.cell(row, collector_expected_value_col).value = result['collector_expected_value']
                sheet.cell(row, collector_market_value_col).value = result['collector_market_value']
                updated_count += 1
                print(f"  ✓ Updated - Expected: {result['collector_expected_value']}, Market: {result['collector_market_value']}")
            else:
                # Clear cells - no matching booster type found
                sheet.cell(row, collector_expected_value_col).value = ''
                sheet.cell(row, collector_market_value_col).value = ''
                collector_cleared_count += 1
                print(f"  ✗ Cleared values - no matching booster type found")
            
            # Small delay between requests
            time.sleep(random.uniform(10, 20))
        
        # Save workbook
        print(f"\n{'='*80}")
        print(f"Saving workbook...")
        wb.save(workbook_name)
        
        print(f"\nComplete!")
        print(f"Processed: {processed_count} entries")
        print(f"Updated: {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()