// yahoo_company_extraction.js
// JavaScript extraction script for Yahoo Finance company details
// Used to extract ticker, sector, and exchange from Yahoo Finance search results
// Only ticker is mandatory - sector and exchange are optional fields

// Example selectors:
// with results:
// document.querySelector("#main-content-wrapper > section > section.container.yf-1omxedn > div.tableContainer.yf-1omxedn > div > table")
// document.querySelector("#\\30  > td:nth-child(1) > span > div > a")
// document.querySelector("#\\30  > td:nth-child(2) > span > div")
// document.querySelector("#\\30  > td:nth-child(3) > span > div")
// document.querySelector("#\\30  > td:nth-child(4) > span > div > a")
// document.querySelector("#\\30  > td:nth-child(5) > span > div")
// document.querySelector("#\\30  > td:nth-child(6) > span > div")
// row with no result:
// document.querySelector("#\\32  > td:nth-child(4) > span > p")
// no results:
// document.querySelector("#main-content-wrapper > section > div.noData.yf-1omxedn")

// Using a wrapper to ensure the result is properly captured
var extractionResult = (function() {
    try {
        // Check for "No results found" message using exact selector
        const noDataElement = document.querySelector('#main-content-wrapper > section > div.noData.yf-1omxedn');
        if (noDataElement) {
            return { status: 'no_results', ticker: null, sector: null, exchange: null };
        }

        // Find the results table using exact selector
        const table = document.querySelector('#main-content-wrapper > section > section.container.yf-1omxedn > div.tableContainer.yf-1omxedn > div > table');
        if (!table) {
            return { status: 'no_results', ticker: null, sector: null, exchange: null };
        }

        // Find all rows in tbody
        const allRows = table.querySelectorAll('tbody tr');
        if (!allRows || allRows.length === 0) {
            return { status: 'no_results', ticker: null, sector: null, exchange: null };
        }

        // Helper function to safely extract text content
        function extractText(element) {
            if (!element) return '';
            const text = element.textContent.trim();
            return text;
        }

        // Helper function to check if a cell actually contains data
        // Multiple indicators are used to determine if data is present
        function hasValidData(cellElement) {
            if (!cellElement) return false;

            // Indicator 1: Check if the cell contains a <p> tag (Yahoo uses this for "no data")
            const pTag = cellElement.querySelector('p');
            if (pTag) return false;

            // Indicator 2: Check the direct child structure
            // Valid data cells have: td > span > div or td > span > div > a
            // Invalid data cells have: td > span > p
            const span = cellElement.querySelector('span');
            if (span) {
                const directChildren = Array.from(span.children);
                // If the only or first child is a <p>, it's likely "no data"
                if (directChildren.length > 0 && directChildren[0].tagName === 'P') {
                    return false;
                }
            }

            // Indicator 3: Check text content
            const text = extractText(cellElement);
            if (!text) return false;
            const normalized = text.toLowerCase().trim();
            
            // Common "no data" indicators
            const noDataIndicators = [
                '-',
                'n/a',
                'na',
                'none',
                'not available',
                'no data',
                '--',
                '—', // em dash
                '–', // en dash
            ];
            
            if (noDataIndicators.includes(normalized)) {
                return false;
            }

            // Indicator 4: Check for common CSS classes that indicate empty state
            const classIndicators = ['empty', 'no-data', 'na', 'null', 'undefined'];
            const classList = cellElement.className || '';
            for (const indicator of classIndicators) {
                if (classList.includes(indicator)) {
                    return false;
                }
            }

            // Indicator 5: Check if cell has an anchor tag (usually indicates real data)
            const hasLink = cellElement.querySelector('a') !== null;
            
            // Indicator 6: Check if there's actual substantial content
            // If text is very short (1-2 chars) and not alphanumeric, it's likely not real data
            if (text.length <= 2 && !/[a-zA-Z0-9]/.test(text)) {
                return false;
            }

            // If we passed all checks, consider it valid data
            return true;
        }

        // Helper function to extract and normalize data from a cell
        function extractCellData(cellElement) {
            if (!cellElement) return null;
            if (!hasValidData(cellElement)) return null;
            
            const text = extractText(cellElement);
            return text || null;
        }

        // Helper function to extract and normalize data from a row
        function extractRowData(row) {
            // Extract ticker from column 1 (td:nth-child(1))
            const tickerCell = row.querySelector('td:nth-child(1)');
            const ticker = extractCellData(tickerCell);

            // Extract sector from column 4 (td:nth-child(4))
            const sectorCell = row.querySelector('td:nth-child(4)');
            const sector = extractCellData(sectorCell);

            // Extract exchange from column 6 (td:nth-child(6))
            const exchangeCell = row.querySelector('td:nth-child(6)');
            const exchange = extractCellData(exchangeCell);

            return { ticker, sector, exchange };
        }

        // Helper function to count non-null fields (data completeness counter)
        function countValidFields(data) {
            let count = 0;
            if (data.ticker) count++;
            if (data.sector) count++;
            if (data.exchange) count++;
            return count;
        }

        // Helper function to score a row (prioritize rows with more complete data)
        function scoreRow(data) {
            let score = 0;
            
            // Ticker is mandatory and gets highest weight
            if (data.ticker) score += 100;
            
            // Sector and exchange are nice-to-have
            if (data.sector) score += 10;
            if (data.exchange) score += 10;
            
            return score;
        }

        // Extract data from all rows and find the one with most complete data
        let bestRow = null;
        let maxScore = -1;
        let rowIndex = 0;

        for (const row of allRows) {
            const data = extractRowData(row);
            const score = scoreRow(data);
            
            // Select row with highest score (most complete data)
            // If tied, first row wins
            if (score > maxScore) {
                bestRow = data;
                maxScore = score;
                bestRow.rowIndex = rowIndex;
                bestRow.validFieldCount = countValidFields(data);
                bestRow.score = score;
            }
            
            rowIndex++;
        }

        // Ticker is mandatory - return error status if not found
        if (!bestRow || !bestRow.ticker) {
            return { 
                status: 'error', 
                error_message: 'No ticker found in any row',
                ticker: null, 
                sector: null, 
                exchange: null 
            };
        }

        // Return success with ticker (mandatory) and optional sector/exchange
        // Include metadata about which row was selected and how many valid fields it had
        return {
            status: 'found',
            ticker: bestRow.ticker,
            sector: bestRow.sector,
            exchange: bestRow.exchange,
            metadata: {
                selectedRowIndex: bestRow.rowIndex,
                validFieldCount: bestRow.validFieldCount,
                score: bestRow.score,
                totalRows: allRows.length
            }
        };

    } catch (error) {
        // Only catch unexpected errors during extraction
        return {
            status: 'error',
            error_message: error.toString(),
            ticker: null,
            sector: null,
            exchange: null
        };
    }
})();

// Return the result explicitly
return extractionResult;