"""
AkDevz AI Market Intelligence — Market Snapshot Generator
Generates point-in-time immutable evidence packages for quantitative analytics and AI reasoning.
"""
import time
from typing import Dict, Any, List, Optional
from datetime import datetime
from zoneinfo import ZoneInfo

from python.data.historical_collector import HistoricalCollector
from python.data.realtime_collector import RealtimeCollector
from python.data.option_chain import OptionChainCollector
from python.data.market_depth import MarketDepthEngine

IST = ZoneInfo("Asia/Kolkata")

class MarketSnapshotGenerator:
    def __init__(self):
        self.history_collector = HistoricalCollector()
        self.quote_collector = RealtimeCollector()
        self.option_collector = OptionChainCollector()
        self.depth_engine = MarketDepthEngine()

    def generate_snapshot(self, symbol: str) -> Dict[str, Any]:
        """
        Synthesizes a complete validated market snapshot for a given symbol.
        """
        now = datetime.now(IST)
        now_ts = int(time.time())

        # Quotes
        quotes = self.quote_collector.collect_quotes([symbol])
        quote = quotes.get(symbol, {})

        # History (5m default intraday baseline)
        history_res = self.history_collector.fetch_candles(symbol, timeframe="5m", days_back=3)
        candles = history_res.get("candles", [])

        # Options chain
        options = self.option_collector.get_option_chain_snapshot(symbol, strike_count=10)

        # Depth
        depth = self.depth_engine.analyze_depth(symbol)

        snapshot_id = f"snap_{symbol}_{now_ts}"

        return {
            "snapshot_id": snapshot_id,
            "symbol": symbol,
            "timestamp": now.strftime("%Y-%m-%d %H:%M:%S IST"),
            "timestamp_epoch": now_ts,
            "data_source": "FYERS_API_V3",
            "quote": quote,
            "candles_count": len(candles),
            "candles": candles[-50:] if len(candles) > 50 else candles,
            "options": options,
            "depth": depth,
            "freshness": quote.get("freshness", "UNKNOWN")
        }
