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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
"""
US Stock Listings Module - Autocomplete data source.

Data Sources:
- NASDAQ Trader FTP files (nasdaqlisted.txt, otherlisted.txt)
- Cached locally with daily refresh

Includes: NYSE, NASDAQ, AMEX equities only
Excludes: OTC, ETFs, mutual funds, crypto, indices, international
"""

import csv
import json
import logging
import os
import re
from datetime import datetime, timedelta
from pathlib import Path
from typing import List, Optional
from concurrent.futures import ThreadPoolExecutor

import requests

logger = logging.getLogger("stock-listings")

# Cache configuration
CACHE_DIR = Path(__file__).parent.parent / "data" / "cache"
CACHE_FILE = CACHE_DIR / "us_stocks.json"
CACHE_EXPIRY_HOURS = 24

# NASDAQ Trader FTP URLs (publicly accessible)
NASDAQ_LISTED_URL = "https://www.nasdaqtrader.com/dynamic/SymDir/nasdaqlisted.txt"
OTHER_LISTED_URL = "https://www.nasdaqtrader.com/dynamic/SymDir/otherlisted.txt"

# Exclusion patterns
EXCLUDED_SUFFIXES = {
    # ETFs, funds, notes, warrants, units, rights
    "ETF", "FUND", "TRUST", "NOTE", "WARRANT", "UNIT", "RIGHT",
    "PREFERRED", "DEBENTURE", "BOND", "REIT"
}

EXCLUDED_PATTERNS = [
    r'\bETF\b', r'\bETN\b', r'\bETP\b',
    r'\bFUND\b', r'\bTRUST\b', r'\bINDEX\b',
    r'WARRANT', r'RIGHTS?$', r'UNITS?$',
    r'PREFERRED', r'PFD', r'PRF',
    r'\bLP\b$', r'\bLLC\b$',
    r'DEPOSITARY', r'ADR$', r'ADS$',
]

# Suffixes to strip from company names for cleaner display
NAME_SUFFIXES_TO_STRIP = [
    " - Common Stock",
    " - Class A Common Stock",
    " - Class B Common Stock",
    " - Class C Common Stock",
    " - Ordinary Shares",
    " - Class A Ordinary Shares",
    " - Class B Ordinary Shares",
    " Common Stock",
    " Ordinary Shares",
]


def _clean_company_name(name: str) -> str:
    """Strip common suffixes from company names for cleaner display."""
    for suffix in NAME_SUFFIXES_TO_STRIP:
        if name.endswith(suffix):
            return name[:-len(suffix)]
    return name


def _is_common_stock(name: str, symbol: str) -> bool:
    """Filter to include only common stocks, exclude ETFs/funds/etc."""
    name_upper = name.upper()

    # Exclude based on patterns
    for pattern in EXCLUDED_PATTERNS:
        if re.search(pattern, name_upper):
            return False

    # Exclude symbols with special characters (warrants, units, etc.)
    if any(c in symbol for c in ['+', '.', '-', '$']):
        # Allow simple suffixes like BRK.A, BRK.B
        if not re.match(r'^[A-Z]+\.[A-Z]$', symbol):
            return False

    # Exclude very short company names (likely test symbols)
    if len(name) < 3:
        return False

    return True


def _parse_nasdaq_file(content: str, exchange: str) -> List[dict]:
    """Parse NASDAQ trader file format."""
    stocks = []
    lines = content.strip().split('\n')

    if not lines:
        return stocks

    # Skip header and footer
    for line in lines[1:]:
        if line.startswith('File Creation Time'):
            continue

        fields = line.split('|')
        if len(fields) < 2:
            continue

        if exchange == "NASDAQ":
            # nasdaqlisted.txt format: Symbol|Security Name|Market Category|Test Issue|Financial Status|Round Lot Size|ETF|NextShares
            symbol = fields[0].strip()
            name = fields[1].strip()
            is_etf = fields[6].strip().upper() == 'Y' if len(fields) > 6 else False
            is_test = fields[3].strip().upper() == 'Y' if len(fields) > 3 else False

            if is_etf or is_test:
                continue

        else:
            # otherlisted.txt format: ACT Symbol|Security Name|Exchange|CQS Symbol|ETF|Round Lot Size|Test Issue|NASDAQ Symbol
            symbol = fields[0].strip()
            name = fields[1].strip()
            exch_code = fields[2].strip()
            is_etf = fields[4].strip().upper() == 'Y' if len(fields) > 4 else False
            is_test = fields[6].strip().upper() == 'Y' if len(fields) > 6 else False

            if is_etf or is_test:
                continue

            # Map exchange codes
            exchange = {
                'A': 'AMEX',
                'N': 'NYSE',
                'P': 'NYSE ARCA',
                'Z': 'BATS',
                'V': 'IEX'
            }.get(exch_code, exch_code)

            # Only include major US exchanges
            if exchange not in ['NYSE', 'AMEX', 'NYSE ARCA']:
                continue

        # Filter common stocks only
        if not _is_common_stock(name, symbol):
            continue

        stocks.append({
            "symbol": symbol,
            "name": _clean_company_name(name),
            "exchange": exchange
        })

    return stocks


def _fetch_listings() -> List[dict]:
    """Fetch stock listings from NASDAQ trader files."""
    stocks = []

    try:
        # Fetch NASDAQ listed
        logger.info("Fetching NASDAQ listings...")
        resp = requests.get(NASDAQ_LISTED_URL, timeout=30)
        resp.raise_for_status()
        nasdaq_stocks = _parse_nasdaq_file(resp.text, "NASDAQ")
        stocks.extend(nasdaq_stocks)
        logger.info(f"Parsed {len(nasdaq_stocks)} NASDAQ stocks")

        # Fetch other exchanges (NYSE, AMEX)
        logger.info("Fetching NYSE/AMEX listings...")
        resp = requests.get(OTHER_LISTED_URL, timeout=30)
        resp.raise_for_status()
        other_stocks = _parse_nasdaq_file(resp.text, "OTHER")
        stocks.extend(other_stocks)
        logger.info(f"Parsed {len(other_stocks)} NYSE/AMEX stocks")

    except Exception as e:
        logger.error(f"Error fetching listings: {e}")
        # Return cached data if available
        if CACHE_FILE.exists():
            return _load_cache()
        raise

    # Remove duplicates by symbol
    seen = set()
    unique_stocks = []
    for stock in stocks:
        if stock["symbol"] not in seen:
            seen.add(stock["symbol"])
            unique_stocks.append(stock)

    return unique_stocks


def _enrich_with_market_cap(stocks: List[dict], max_workers: int = 10) -> List[dict]:
    """
    Enrich stock data with market cap for ranking.
    Uses yfinance in parallel for speed.
    Only enriches top stocks by name length (proxy for major companies).
    """
    try:
        import yfinance as yf
    except ImportError:
        logger.warning("yfinance not installed, skipping market cap enrichment")
        for stock in stocks:
            stock["market_cap"] = 0
        return stocks

    def fetch_market_cap(symbol: str) -> Optional[float]:
        try:
            ticker = yf.Ticker(symbol)
            info = ticker.info
            return info.get("marketCap", 0) or 0
        except:
            return 0

    # For performance, only fetch market cap for a subset
    # In production, this would be pre-computed and cached
    logger.info("Enriching with market cap data (sampling)...")

    # Sample: fetch for symbols with short names (likely major companies)
    symbols_to_fetch = [s["symbol"] for s in stocks if len(s["symbol"]) <= 4][:200]

    market_caps = {}
    with ThreadPoolExecutor(max_workers=max_workers) as executor:
        results = list(executor.map(fetch_market_cap, symbols_to_fetch))
        for symbol, cap in zip(symbols_to_fetch, results):
            market_caps[symbol] = cap

    # Apply market caps
    for stock in stocks:
        stock["market_cap"] = market_caps.get(stock["symbol"], 0)

    return stocks


def _save_cache(stocks: List[dict]):
    """Save stocks to cache file."""
    CACHE_DIR.mkdir(parents=True, exist_ok=True)
    cache_data = {
        "updated_at": datetime.now().isoformat(),
        "count": len(stocks),
        "stocks": stocks
    }
    with open(CACHE_FILE, 'w') as f:
        json.dump(cache_data, f)
    logger.info(f"Cached {len(stocks)} stocks to {CACHE_FILE}")


def _load_cache() -> List[dict]:
    """Load stocks from cache file."""
    if not CACHE_FILE.exists():
        return []

    with open(CACHE_FILE, 'r') as f:
        cache_data = json.load(f)

    return cache_data.get("stocks", [])


def _is_cache_valid() -> bool:
    """Check if cache exists and is not expired."""
    if not CACHE_FILE.exists():
        return False

    try:
        with open(CACHE_FILE, 'r') as f:
            cache_data = json.load(f)

        updated_at = datetime.fromisoformat(cache_data.get("updated_at", ""))
        expiry = updated_at + timedelta(hours=CACHE_EXPIRY_HOURS)
        return datetime.now() < expiry
    except:
        return False


def get_us_stock_listings(force_refresh: bool = False) -> List[dict]:
    """
    Get list of US stock listings.

    Returns list of dicts with: symbol, name, exchange, market_cap
    Cached for 24 hours.
    """
    if not force_refresh and _is_cache_valid():
        logger.info("Loading stocks from cache")
        return _load_cache()

    logger.info("Fetching fresh stock listings...")
    stocks = _fetch_listings()

    # Sort by market cap (descending) then alphabetically
    stocks.sort(key=lambda x: (-x.get("market_cap", 0), x["symbol"]))

    _save_cache(stocks)
    return stocks


def search_stocks(
    query: str,
    stocks: List[dict],
    max_results: int = 10,
    min_query_length: int = 1
) -> List[dict]:
    """
    Search stocks by name or symbol.

    Features:
    - Case-insensitive matching
    - Matches start of symbol (higher priority) or contains in name
    - Returns results ranked by: exact match > symbol prefix > name contains > market cap

    Args:
        query: Search string
        stocks: List of stock dicts
        max_results: Maximum results to return
        min_query_length: Minimum query length to trigger search

    Returns:
        List of matching stocks with 'match_type' and 'match_indices' for highlighting
    """
    if not query or len(query) < min_query_length:
        return []

    query_upper = query.upper().strip()
    query_lower = query.lower().strip()
    results = []

    for stock in stocks:
        symbol = stock["symbol"].upper()
        name = stock["name"]
        name_lower = name.lower()

        match_type = None
        match_indices = []

        # Priority 1: Exact symbol match
        if symbol == query_upper:
            match_type = "exact_symbol"
            match_indices = list(range(len(query)))

        # Priority 2: Symbol starts with query
        elif symbol.startswith(query_upper):
            match_type = "symbol_prefix"
            match_indices = list(range(len(query)))

        # Priority 3: Symbol contains query
        elif query_upper in symbol:
            match_type = "symbol_contains"
            start = symbol.find(query_upper)
            match_indices = list(range(start, start + len(query)))

        # Priority 4: Name starts with query
        elif name_lower.startswith(query_lower):
            match_type = "name_prefix"
            match_indices = list(range(len(query)))

        # Priority 5: Name contains query (word boundary preferred)
        elif query_lower in name_lower:
            match_type = "name_contains"
            start = name_lower.find(query_lower)
            match_indices = list(range(start, start + len(query)))

        if match_type:
            results.append({
                **stock,
                "match_type": match_type,
                "match_indices": match_indices
            })

    # Sort by match priority, then market cap
    priority = {
        "exact_symbol": 0,
        "symbol_prefix": 1,
        "symbol_contains": 2,
        "name_prefix": 3,
        "name_contains": 4
    }

    results.sort(key=lambda x: (
        priority.get(x["match_type"], 99),
        -x.get("market_cap", 0),
        x["symbol"]
    ))

    return results[:max_results]


def highlight_match(text: str, query: str, is_symbol: bool = False) -> str:
    """
    Return text with HTML highlighting for matched portions.

    Args:
        text: Original text
        query: Search query
        is_symbol: If True, match from start; if False, match anywhere

    Returns:
        HTML string with <mark> tags around matches
    """
    if not query:
        return text

    query_lower = query.lower()
    text_lower = text.lower()

    if query_lower not in text_lower:
        return text

    start = text_lower.find(query_lower)
    end = start + len(query)

    return f"{text[:start]}<mark>{text[start:end]}</mark>{text[end:]}"


# Pre-load function for Streamlit caching
def init_stock_listings():
    """Initialize stock listings (call once at app start)."""
    return get_us_stock_listings()


if __name__ == "__main__":
    # Test the module
    logging.basicConfig(level=logging.INFO)

    print("Fetching US stock listings...")
    stocks = get_us_stock_listings(force_refresh=True)
    print(f"Total stocks: {len(stocks)}")

    # Test search
    test_queries = ["AAPL", "Apple", "TSLA", "Micro", "goo"]
    for q in test_queries:
        results = search_stocks(q, stocks, max_results=5)
        print(f"\nSearch '{q}':")
        for r in results:
            print(f"  {r['symbol']:6} | {r['name'][:40]:40} | {r['exchange']} | {r['match_type']}")