"""Pure, non-authorizing lifecycle transition simulator.

This planner is deliberately not wired into live strategy selection. It makes
transition policy testable without granting any strategy betting authority.
"""

GATES = ('data', 'historical_roi', 'statistical', 'forward', 'risk', 'stability')
STATUSES = ('CANDIDATE', 'PROBATION', 'ACTIVE', 'DEMOTED')


def plan_transition(previous, evidence, *, prior_passes=0, prior_failures=0,
                    promotion_confirmations=2, demotion_confirmations=2):
    """Return a proposed status and counters, never a live authorization.

    Confirmation counts must come from *distinct, chronological, new* evidence
    revisions; the caller must enforce that requirement. The function itself
    has no access to the registry and cannot prove revision independence.
    """
    if previous is not None and previous not in STATUSES:
        raise ValueError('Unknown lifecycle status')
    if not isinstance(evidence, dict) or evidence.get('activation_authorized') is not False:
        raise ValueError('Explicitly non-authorizing evidence required')
    gates = evidence.get('gates')
    if not isinstance(gates, dict) or any(type(gates.get(g, {}).get('pass')) is not bool
                                               for g in GATES if isinstance(gates.get(g), dict)) or any(not isinstance(gates.get(g), dict) for g in GATES):
        raise ValueError('Six explicit Boolean gates required')
    settled = evidence.get('historical', {}).get('settled')
    if type(settled) is not int or settled < 0:
        raise ValueError('Invalid settled match count')
    if any(type(v) is not int or v < 0 for v in (prior_passes, prior_failures)):
        raise ValueError('Invalid confirmation counters')
    if promotion_confirmations < 2 or demotion_confirmations < 2:
        raise ValueError('Hysteresis requires at least two confirmations')
    all_pass = settled >= 50 and all(gates[g]['pass'] for g in GATES)
    passes = prior_passes + 1 if all_pass else 0
    failures = 0 if all_pass else prior_failures + 1
    previous = previous or 'CANDIDATE'
    if previous == 'ACTIVE':
        proposed = 'DEMOTED' if failures >= demotion_confirmations else 'ACTIVE'
    elif previous == 'DEMOTED':
        proposed = 'ACTIVE' if passes >= promotion_confirmations else 'DEMOTED'
    elif previous == 'CANDIDATE':
        proposed = 'PROBATION' if all_pass else 'CANDIDATE'
    else:
        proposed = 'ACTIVE' if passes >= promotion_confirmations else ('CANDIDATE' if not all_pass else 'PROBATION')
    return {'previous': previous, 'proposed': proposed, 'consecutive_passes': passes,
            'consecutive_failures': failures, 'all_gates_pass': all_pass,
            'live_authorized': False, 'simulation_only': True}
