"""AlphaBot V2 callbacks. Decimal amounts; one entry per observation month."""
from decimal import Decimal


def capital_floor(starting_capital, asset_cost, peak_equity):
    """Entry fees are already included in asset_cost; never subtract them twice."""
    minimum = Decimal('250')
    milestone = Decimal('1000')
    while peak_equity >= milestone:
        minimum = milestone / 2
        milestone *= 2
    return max(minimum, starting_capital - asset_cost)


def decide(context):
    """Return a declarative action; execution and safety belong to the runner."""
    if context['tone'] is not None and context['tone'] < Decimal('-5'):
        return {'action': 'liquidate', 'reason': 'geopolitical_tone'}
    if context['tone'] is None:
        return {'action': 'hold'}
    values = context['indpro']
    if len(values) != 4 or context['month'] == context['last_entry_month']:
        return {'action': 'hold'}
    if values[-1] > sum(values[:-1]) / 3:
        return {'action': 'buy', 'budget': context['cash'] * Decimal('.20')}
    return {'action': 'hold'}
