import pandas as pd
import dateutil.tz

from tradingo.symbols import symbol_publisher
from tradingo.sampling import get_ig_service


@symbol_publisher(
    template="live/activity.{0}",
    symbol_prefix="{provider}.{universe}.",
)
def get_activity_history(
    from_date,
    to_date,
    svc=None,
    **kwargs,
):
    """
    get activtiy history and return dataframe per asset
    """

    svc = svc or get_ig_service()

    act = svc.fetch_account_activity_by_date(
        from_date=pd.Timestamp(from_date), to_date=pd.Timestamp(to_date)
    )
    act["DateTime"] = (
        pd.to_datetime(act["date"] + " " + act["time"])
        .tz_localize(dateutil.tz.tzlocal())
        .tz_convert("utc")
    )

    return tuple(
        (
            data.set_index("DateTime")
            .sort_index()
            .drop(["date", "time", "period", "epic", "marketName"], axis=1)
            .replace("-", np.nan)
            .astype(
                {
                    "size": float,
                    "stop": float,
                    "limit": float,
                },
                errors="ignore",
            ),
            (epic,),
        )
        for epic, data in iter(act.groupby("epic"))
    )
