"""
AkDevz AI Market Intelligence — FYERS API V3 Capabilities Register
Tracks official entitlements, rate limits, and endpoint schemas for FYERS Standard API access.
"""
from datetime import datetime, timezone
from typing import Dict, Any, List

CAPABILITIES_REGISTRY: Dict[str, Dict[str, Any]] = {
    "profile": {
        "endpoint": "/api/v3/profile",
        "method": "GET",
        "plan_entitlement": "Standard",
        "rate_limit": "5 req/s, 50 req/min, 5000 req/day",
        "status": "AVAILABLE",
        "description": "User account verification and profile information",
        "required_permission": "profile",
        "fields_validated": ["name", "email", "fy_id", "mobile_number"],
    },
    "quotes": {
        "endpoint": "/data/quotes",
        "method": "GET",
        "plan_entitlement": "Standard",
        "rate_limit": "Max 50 symbols per request, 5 req/s",
        "status": "AVAILABLE",
        "description": "Real-time quote snapshot with OHLC, LTP, Volume, Bid, Ask",
        "required_permission": "market_data",
        "fields_validated": ["cmd", "lp", "open_price", "high_price", "low_price", "prev_close_price", "volume"],
    },
    "history": {
        "endpoint": "/data/history",
        "method": "GET",
        "plan_entitlement": "Standard",
        "rate_limit": "1 yr intraday, 5 yr daily, 5 req/s",
        "status": "AVAILABLE",
        "description": "Historical candle data for resolution 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 240, D",
        "required_permission": "historical_data",
        "fields_validated": ["candles"],
    },
    "option_chain": {
        "endpoint": "/data/options-chain-v3",
        "method": "GET",
        "plan_entitlement": "Standard",
        "rate_limit": "5 req/s, 50 req/min, max 50 strikes",
        "status": "AVAILABLE",
        "description": "Strike-wise option chain with LTP, Volume, OI, IV, Delta, Gamma, Theta, Vega",
        "required_permission": "options_chain",
        "fields_validated": ["optionsChain", "expiryData", "strikePrice"],
    },
    "market_depth": {
        "endpoint": "/data/depth",
        "method": "GET",
        "plan_entitlement": "Standard",
        "rate_limit": "5 req/s, 50 req/min",
        "status": "AVAILABLE",
        "description": "Level-2 Market depth with top 5 bids and asks",
        "required_permission": "market_data",
        "fields_validated": ["bids", "asks", "totalbuyqty", "totalsellqty"],
    },
    "websocket": {
        "endpoint": "wss://socket.fyers.in/data-v3",
        "method": "WSS",
        "plan_entitlement": "Standard",
        "rate_limit": "Max 100 symbols simultaneous subscription",
        "status": "AVAILABLE",
        "description": "Real-time tick data stream with auto-reconnection and symbol subscriptions",
        "required_permission": "websocket_data",
        "fields_validated": ["symbol", "ltp", "vol_traded_today", "last_traded_time"],
    },
    "order_placement": {
        "endpoint": "NONE — ZERO REAL BROKER ORDER EXECUTION",
        "method": "DISABLED",
        "plan_entitlement": "N/A",
        "rate_limit": "0 req/s",
        "status": "STRICTLY_BLOCKED",
        "description": "Mandatory safety rule: AkDevz AI Market Intelligence is strictly analysis-only. No trading endpoints.",
        "required_permission": "NONE",
        "fields_validated": [],
    }
}

class FyersCapabilities:
    """Provides authoritative inspection of capabilities and verified boundaries."""

    @staticmethod
    def get_all_capabilities() -> Dict[str, Dict[str, Any]]:
        return CAPABILITIES_REGISTRY

    @staticmethod
    def is_feature_available(feature_name: str) -> bool:
        cap = CAPABILITIES_REGISTRY.get(feature_name)
        if not cap:
            return False
        return cap.get("status") == "AVAILABLE"

    @staticmethod
    def get_feature_spec(feature_name: str) -> Dict[str, Any]:
        return CAPABILITIES_REGISTRY.get(feature_name, {
            "status": "UNKNOWN",
            "description": f"Feature {feature_name} is not in the FYERS API V3 standard registry."
        })

    @staticmethod
    def verify_order_execution_blocked() -> bool:
        """Absolute architectural rule: ensure order placement capability is strictly blocked."""
        cap = CAPABILITIES_REGISTRY.get("order_placement", {})
        return cap.get("status") == "STRICTLY_BLOCKED"
