from __future__ import annotations

from dataclasses import dataclass

from kraken_bot.config import PairConfig, RiskConfig


@dataclass(frozen=True)
class OrderPlan:
    quote_amount: float
    base_volume: float


class RiskSizer:
    def __init__(self, config: RiskConfig) -> None:
        self.config = config

    def plan_order(self, pair: PairConfig, price: float) -> OrderPlan:
        quote_amount = min(self.config.fixed_order_quote, self.config.max_order_quote)
        if pair.max_order_quote is not None:
            quote_amount = min(quote_amount, pair.max_order_quote)
        base_volume = round(quote_amount / price, self.config.volume_precision)
        if base_volume < pair.min_order_base:
            raise ValueError(
                f"{pair.symbol} order volume {base_volume} is below min_order_base {pair.min_order_base}"
            )
        return OrderPlan(round(quote_amount, self.config.price_precision), base_volume)
