# piker: trading gear for hackers
# Copyright (C) 2018-present Tyler Goodlet (in stewardship of pikers)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

"""
Handy cross-broker utils.

"""
from __future__ import annotations
# from functools import partial
from typing import (
    Type,
)

import json
import httpx
import logging
from msgspec import Struct
from tractor._exceptions import (
    reg_err_types,
)

from piker.log import (
    colorize_json,
)
subsys: str = 'piker.brokers'

# NOTE: level should be reset by any actor that is spawned
# as well as given a (more) explicit name/key such
# as `piker.brokers.binance` matching the subpkg.
# log = get_logger(subsys)

# ?TODO?? we could use this approach, but we need to be able
# to pass multiple `name=` values so for example we can include the
# emissions in `.accounting._pos` and others!
# [ ] maybe we could do the `log = get_logger()` above,
# then cycle through the list of subsys mods we depend on
# and then get all their loggers and pass them to
# `get_console_log(logger=)`??
# [ ] OR just write THIS `get_console_log()` as a hook which does
# that based on who calls it?.. i dunno
#
# get_console_log = partial(
#     get_console_log,
#     name=subsys,
# )


class BrokerError(Exception):
    "Generic broker issue"


class SymbolNotFound(BrokerError):
    "Symbol not found by broker search"


class MarketNotFound(SymbolNotFound):
    "Mkt-pair not found by broker search"


# TODO: these should probably be moved to `.tsp/.data`?
class NoData(BrokerError):
    '''
    Symbol data not permitted or no data
    for time range found.

    '''
    def __init__(
        self,
        *args,
        info: dict|None = None,

    ) -> None:
        super().__init__(*args)
        self.info: dict|None = info

        # when raised, machinery can check if the backend
        # set a "frame size" for doing datetime calcs.
        # self.frame_size: int = 1000


class DataUnavailable(BrokerError):
    '''
    Signal storage requests to terminate.

    '''
    # TODO: add in a reason that can be displayed in the
    # UI (for eg. `kraken` is bs and you should complain
    # to them that you can't pull more OHLC data..)


class DataThrottle(BrokerError):
    '''
    Broker throttled request rate for data.

    '''
    # TODO: add in throttle metrics/feedback

class SchemaMismatch(BrokerError):
    '''
    Market `Pair` fields mismatch, likely due to provider API update.

    '''

# auto-register all `BrokerError` subtypes for
# tractor IPC exc-marshalling.
reg_err_types([
    BrokerError,
    *BrokerError.__subclasses__(),
])


def resproc(
    resp: httpx.Response,
    log: logging.Logger,
    return_json: bool = True,
    log_resp: bool = False,

) -> httpx.Response:
    '''
    Process response and return its json content.

    Raise the appropriate error on non-200 OK responses.

    '''
    if not resp.status_code == 200:
        raise BrokerError(resp.body)
    try:
        msg = resp.json()
    except json.decoder.JSONDecodeError:
        log.exception(f"Failed to process {resp}:\n{resp.text}")
        raise BrokerError(resp.text)

    if log_resp:
        log.debug(f"Received json contents:\n{colorize_json(msg)}")

    return msg if return_json else resp


def get_or_raise_on_pair_schema_mismatch(
    pair_type: Type[Struct],
    fields_data: dict,
    provider_name: str,
    api_url: str|None = None,
) -> Struct:
    '''
    Boilerplate helper around assset-`Pair` field schema mismatches,
    normally due to provider API updates.

    '''
    try:
        pair: Struct = pair_type(**fields_data)
        return pair
    except TypeError as err:

        from tractor.devx.pformat import ppfmt
        repr_data: str = ppfmt(fields_data)
        report: str = (
            f'Field mismatch we need to codify!\n'
            f'\n'
            f'{pair_type!r}({repr_data})'
            f'\n'
            f'^^^ {err.args[0]!r} ^^^\n'
            f'\n'
            f"Don't panic, prolly {provider_name!r} "
            f"changed their symbology schema..\n"
        )
        if (
            api_url
            or
            (api_url := pair_type._api_url)
        ):
            report += (
                f'\n'
                f'Check out their API docs here:\n'
                f'{api_url}\n'
            )

        raise SchemaMismatch(report) from err
