from typing import Dict, Any, Optional, List
from pydantic import BaseModel, Field


class IndicatorInstance(BaseModel):
    """
    Represents an instance of an indicator applied to a chart.

    This schema holds both the TA-Lib metadata and TradingView-specific data
    needed for synchronization.
    """
    id: str = Field(..., description="Unique identifier for this indicator instance")

    # TA-Lib metadata
    talib_name: str = Field(..., description="TA-Lib indicator name (e.g., 'RSI', 'SMA', 'MACD')")
    instance_name: str = Field(..., description="User-friendly instance name")
    parameters: Dict[str, Any] = Field(default_factory=dict, description="TA-Lib indicator parameters")

    # TradingView metadata
    tv_study_id: Optional[str] = Field(default=None, description="TradingView study ID assigned by the chart widget")
    tv_indicator_name: Optional[str] = Field(default=None, description="TradingView indicator name if different from TA-Lib")
    tv_inputs: Optional[Dict[str, Any]] = Field(default=None, description="TradingView-specific input parameters")

    # Visual properties
    visible: bool = Field(default=True, description="Whether indicator is visible on chart")
    pane: str = Field(default="chart", description="Pane where indicator is displayed ('chart' or 'separate')")

    # Metadata
    symbol: Optional[str] = Field(default=None, description="Symbol this indicator is applied to")
    created_at: Optional[int] = Field(default=None, description="Creation timestamp (Unix seconds)")
    modified_at: Optional[int] = Field(default=None, description="Last modification timestamp (Unix seconds)")
    original_id: Optional[str] = Field(default=None, description="Original ID from backend before TradingView assigns its own ID")


class IndicatorCollection(BaseModel):
    """Collection of all indicator instances on the chart."""
    indicators: Dict[str, IndicatorInstance] = Field(
        default_factory=dict,
        description="Dictionary of indicator instances keyed by ID"
    )
