"""Exchange-level constants — URLs, headers, timeouts, limits.

Keep all magic strings and numbers here. Never hardcode them in business logic.
"""

from __future__ import annotations

# ── Exchange Base URLs ────────────────────────────────────────────────────────

NSE_BASE_URL: str = "https://www.nseindia.com"
BSE_BASE_URL: str = "https://api.bseindia.com"

# ── NSE Endpoint Paths ────────────────────────────────────────────────────────

NSE_OPTION_CHAIN_PATH: str = "/api/option-chain-v3"
NSE_CONTRACT_INFO_PATH: str = "/api/option-chain-contract-info"
NSE_QUOTE_PATH: str = "/api/quote-equity"
NSE_INDEX_PATH: str = "/api/allIndices"
NSE_MARKET_STATUS_PATH: str = "/api/marketStatus"
NSE_HOLIDAYS_PATH: str = "/api/holiday-master?type=trading"

NSE_WARMUP_URLS: tuple[str, ...] = (
    NSE_BASE_URL,
    f"{NSE_BASE_URL}/option-chain",
)

# ── BSE Endpoint Paths ────────────────────────────────────────────────────────

BSE_OPTION_CHAIN_PATH: str = "/BseIndiaAPI/api/DerivOptionChain_IV/w"
BSE_MARKET_STATUS_PATH: str = "/BseIndiaAPI/api/getScripHeaderData/w"

# ── Default HTTP Settings ─────────────────────────────────────────────────────

DEFAULT_WARMUP_TIMEOUT_S: float = 10.0
DEFAULT_FETCH_TIMEOUT_S: float = 15.0
DEFAULT_ASYNC_TIMEOUT_S: float = 45.0

DEFAULT_MAX_RETRIES: int = 5
DEFAULT_BACKOFF_BASE: float = 1.5   # seconds; used as: base ** attempt
DEFAULT_BACKOFF_CAP_S: float = 30.0  # never sleep longer than this
DEFAULT_BACKOFF_JITTER: float = 1.0  # uniform random jitter added

# ── Rate-limit status codes ───────────────────────────────────────────────────

RATE_LIMIT_STATUS_CODES: frozenset[int] = frozenset({401, 403, 429})

# ── Circuit Breaker ───────────────────────────────────────────────────────────

DEFAULT_CIRCUIT_FAILURE_THRESHOLD: int = 5
DEFAULT_CIRCUIT_BACKOFF_S: float = 300.0  # 5 minutes

# ── Cache ─────────────────────────────────────────────────────────────────────

DEFAULT_CACHE_TTL_S: int = 60           # live quote / option chain
DEFAULT_EXPIRY_CACHE_TTL_S: int = 3600  # expiry lists change infrequently

# ── Thread Pool ───────────────────────────────────────────────────────────────

DEFAULT_THREAD_POOL_WORKERS: int = 8

# ── User Agents ───────────────────────────────────────────────────────────────

USER_AGENTS: tuple[str, ...] = (
    # Chrome 124 on Windows
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
    "AppleWebKit/537.36 (KHTML, like Gecko) "
    "Chrome/124.0.0.0 Safari/537.36",
    # Chrome 124 on macOS
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
    "AppleWebKit/537.36 (KHTML, like Gecko) "
    "Chrome/124.0.0.0 Safari/537.36",
    # Chrome 124 on Linux
    "Mozilla/5.0 (X11; Linux x86_64) "
    "AppleWebKit/537.36 (KHTML, like Gecko) "
    "Chrome/124.0.0.0 Safari/537.36",
    # Firefox 125 on Windows
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) "
    "Gecko/20100101 Firefox/125.0",
    # Safari 17 on iPhone
    "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X) "
    "AppleWebKit/605.1.15 (KHTML, like Gecko) "
    "Version/17.4.1 Mobile/15E148 Safari/604.1",
)

# ── NSE Insight Engine Keywords ───────────────────────────────────────────────

BULLISH_KEYWORDS: tuple[str, ...] = (
    "rally", "surge", "gain", "bullish", "up", "positive", "recovery",
    "breakout", "support", "buying", "inflow", "growth", "gdp",
)

BEARISH_KEYWORDS: tuple[str, ...] = (
    "fall", "drop", "decline", "bearish", "down", "negative", "crash",
    "selloff", "resistance", "outflow", "recession", "inflation", "rbi tightening",
)

MARKET_FILTER_KEYWORDS: tuple[str, ...] = (
    "nifty", "banknifty", "sensex", "nse", "bse", "rbi", "sebi",
    "market", "stock", "equity", "index", "inflation", "gdp", "rate",
    "ipo", "fii", "dii", "f&o", "options", "futures",
)

# ── Supported Exchanges ───────────────────────────────────────────────────────

SUPPORTED_EXCHANGES: frozenset[str] = frozenset({"NSE", "BSE"})

__all__ = [
    "BEARISH_KEYWORDS",
    "BSE_BASE_URL",
    "BSE_MARKET_STATUS_PATH",
    "BSE_OPTION_CHAIN_PATH",
    "BULLISH_KEYWORDS",
    "DEFAULT_ASYNC_TIMEOUT_S",
    "DEFAULT_BACKOFF_BASE",
    "DEFAULT_BACKOFF_CAP_S",
    "DEFAULT_BACKOFF_JITTER",
    "DEFAULT_CACHE_TTL_S",
    "DEFAULT_CIRCUIT_BACKOFF_S",
    "DEFAULT_CIRCUIT_FAILURE_THRESHOLD",
    "DEFAULT_EXPIRY_CACHE_TTL_S",
    "DEFAULT_FETCH_TIMEOUT_S",
    "DEFAULT_MAX_RETRIES",
    "DEFAULT_THREAD_POOL_WORKERS",
    "DEFAULT_WARMUP_TIMEOUT_S",
    "MARKET_FILTER_KEYWORDS",
    "NSE_BASE_URL",
    "NSE_CONTRACT_INFO_PATH",
    "NSE_HOLIDAYS_PATH",
    "NSE_INDEX_PATH",
    "NSE_MARKET_STATUS_PATH",
    "NSE_OPTION_CHAIN_PATH",
    "NSE_QUOTE_PATH",
    "NSE_WARMUP_URLS",
    "RATE_LIMIT_STATUS_CODES",
    "SUPPORTED_EXCHANGES",
    "USER_AGENTS",
]
