from math import isfinite
from datetime import date

def _num(x): return isinstance(x,(int,float)) and not isinstance(x,bool) and isfinite(x)
def _r(x): return round(float(x)+0.0,2)
EXPECTED_SCOPE={'unit_cost','payment_cost','support_cost','variable_cost'}

def _out(state,vals,reason):
    if vals is None:
        return {'decision_state':state,'effective_price':None,'profit_amount':None,'markup_pct':None,'margin_pct':None,'minimum_price':None,'reason_code':reason}
    return {'decision_state':state,'effective_price':vals['effective_price'],'profit_amount':vals['profit_amount'],'markup_pct':vals['markup_pct'],'margin_pct':vals['margin_pct'],'minimum_price':vals['minimum_price'],'reason_code':reason}

def _calc(c):
    nums=['unit_cost','selling_price','discount_pct','payment_cost','support_cost','variable_cost','minimum_margin']
    if any(not _num(c.get(k)) for k in nums): return None
    if any(c[k] < 0 for k in ['unit_cost','selling_price','payment_cost','support_cost','variable_cost']): return None
    if not (0 <= c['discount_pct'] < 100): return None
    if not (0 <= c['minimum_margin'] < 100): return None
    governed=_r(c['unit_cost']+c['payment_cost']+c['support_cost']+c['variable_cost'])
    effective=_r(c['selling_price']*(1-c['discount_pct']/100))
    if governed<=0 or effective<=0: return None
    profit=_r(effective-governed)
    markup=_r(profit/governed*100)
    margin=_r(profit/effective*100)
    if c.get('floor_basis')!='TARGET_MARGIN_ON_EFFECTIVE_PRICE': return None
    min_price=_r(governed/(1-c['minimum_margin']/100))
    return {'governed_cost':governed,'effective_price':effective,'profit_amount':profit,'markup_pct':markup,'margin_pct':margin,'minimum_price':min_price}

def evaluate(c):
    decisive=['price_id','unit_cost','selling_price','discount_pct','payment_cost','support_cost','variable_cost','allocated_cost_scope','floor_basis','minimum_margin','evidence_ref']
    if any(c.get(k) in (None,'',[]) for k in decisive): return _out('HOLD',None,'DECISIVE_EVIDENCE_MISSING')
    scope=c.get('allocated_cost_scope')
    if not isinstance(scope,list) or set(scope)!=EXPECTED_SCOPE:
        return _out('DISPUTED',None,'GOVERNED_COST_SCOPE_MISMATCH')
    vals=_calc(c)
    if vals is None: return _out('HOLD',None,'INVALID_NUMERIC_OR_FLOOR_EVIDENCE')
    below=vals['effective_price']+1e-9 < vals['minimum_price']
    if c.get('post_approval_discount') and c.get('approved_before_discount') and below:
        return _out('REPRICE',vals,'POST_APPROVAL_DISCOUNT_BREAKS_FLOOR')
    if not below:
        return _out('ACCEPT',vals,'PRICE_CLEARS_TARGET_MARGIN_FLOOR')
    approved=c.get('exception_approved') is True
    owner=c.get('exception_owner')
    expiry=c.get('exception_expiry')
    asof=c.get('as_of','2026-09-15')
    if approved:
        if not owner or not expiry:
            return _out('REPRICE',vals,'EXCEPTION_INCOMPLETE')
        try:
            exp=date.fromisoformat(expiry); now=date.fromisoformat(asof)
        except Exception:
            return _out('HOLD',vals,'INVALID_EXCEPTION_DATE')
        if exp < now:
            return _out('REPRICE',vals,'EXCEPTION_EXPIRED')
        return _out('EXCEPTION',vals,'VALID_TIME_BOUNDED_EXCEPTION')
    return _out('REPRICE',vals,'PRICE_BELOW_TARGET_MARGIN_FLOOR')
