"""K-line (OHLC) endpoint."""

from __future__ import annotations

from typing import Dict, Any

from sd_api.client.base import BaseClient
from sd_api.models.kline import KlineBar, KlineSeries


class KlineMixin:
    """Endpoints under /api/kline."""

    TF_MAP = {"5m": "5min", "10m": "10min", "1h": "1hour"}

    def __init__(self, base: BaseClient) -> None:
        self._base = base

    def get(
        self,
        symbol: str,
        *,
        tf: str = "5min",
        limit: int = 100,
    ) -> KlineSeries:
        """GET /api/kline — OHLC bars from in-memory K-line cache.

        Parameters
        ----------
        symbol : str
            Product symbol (e.g. "XAUUSD").
        tf : str
            Timeframe: "5min" (default), "10min", or "1hour".
            Short aliases "5m", "10m", "1h" are also accepted.
        limit : int
            Maximum number of bars (default: 100).

        Returns
        -------
        KlineSeries
        """
        symbol = symbol.upper().strip()
        tf = self.TF_MAP.get(tf, tf)  # normalize aliases
        params: Dict[str, Any] = {"symbol": symbol, "tf": tf, "limit": limit}
        data = self._base.get("/api/kline", params=params)
        return KlineSeries.from_api(data)
