"""Offline ML-C14 decision practice. Python 3.13+, standard library only.

No networking, credential inputs, hardware writes or filesystem writes.
Choose a route AND a fallback for each stated scenario, not for every product.
"""
import argparse

ROUTES=('usb','softap','ble','smartconfig','pause')
FALLBACKS=('usb-app','usb-console','service-visit')
CASES={
 'desk':dict(prompt='At your desk: C09 MicroPython app, computer and a proven data cable. You need repeatable development setup, with no phone app.',route='usb',fallback='usb-app',why='Direct USB avoids a radio setup dependency. Verify current STA+IP, then restart without resending.',fallback_why='Keep the runtime port and C09 helper available; clear only its named lab file if needed.'),
 'first-run':dict(prompt='A first-time user has a Wi-Fi phone and no provisioning app. C11 protected SoftAP receiver is installed. A technician can reach native USB.',route='softap',fallback='usb-app',why='The local browser page fits this user. Join the board setup AP and POST the target router details; a receipt is not STA+IP.',fallback_why='The technician can stop the MicroPython app at USB REPL and reopen setup or use the course helper.'),
 'ble-client':dict(prompt='C12 receiver is installed. A supported desktop with Bluetooth and the matching Bleak client must stay on its existing Wi-Fi. Only isolated training credentials are used.',route='ble',fallback='usb-app',why='The documented BLE service and desktop client fit. Subscribe to TX, verify the contract, then write RX. Check saved, STA+IP and restart separately.',fallback_why='If cache/service recovery fails, use native USB and the C12 MicroPython recovery helper.'),
 'classic':dict(prompt='C13 ESP-IDF receiver is installed and listening. A matching classic ESPTouch phone sender is on the target 2.4 GHz lab network; required packet delivery is allowed.',route='smartconfig',fallback='usb-console',why='The matching classic sender and C13 receiver fit. Observe credentials received, STA+IP and sender acknowledgement separately; stop after the bounded window.',fallback_why='Use the running C13 native USB console: C clears only ml_c13/wifi and restarts. mpremote is not an ESP-IDF console command.'),
 'five-ghz':dict(prompt='C13 receiver is installed, but the available phone can use only a 5 GHz network and there is no compatible 2.4 GHz sender path. Native USB is accessible.',route='pause',fallback='usb-console',why='Do not start classic ESPTouch with a missing required 2.4 GHz sender path. Arrange compatible lab Wi-Fi/sender first; a cable does not add a missing receiver API.',fallback_why='Use the C13 console to inspect redacted state. C is a deliberate named-config clear, not a USB credential uploader. Restore or replace firmware only with the documented backup/recovery workflow.'),
 'field':dict(prompt='Field unit: unknown installed firmware, radio setup unavailable, USB socket physically sealed and no maintenance access. Someone suggests repeated remote erase attempts.',route='pause',fallback='service-visit',why='No verified reachable receiver or recovery interface exists. Stop and arrange physical service; do not promise a radio-only recovery path.',fallback_why='A technician must establish access, identify firmware and back up what can be preserved before a documented repair. Plan accessible USB/service access for future units.')
}

def assess(case_id,route,fallback):
    if case_id not in CASES:raise ValueError('Unknown case')
    if route not in ROUTES:raise ValueError('Unknown route')
    if fallback not in FALLBACKS:raise ValueError('Unknown fallback')
    c=CASES[case_id]
    return {'route_ok':route==c['route'],'fallback_ok':fallback==c['fallback'],
            'passed':route==c['route'] and fallback==c['fallback'],
            'reason':c['why'],'recovery':c['fallback_why'],
            'expected_route':c['route'],'expected_fallback':c['fallback']}

def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--case',choices=CASES)
    parser.add_argument('--route',choices=ROUTES)
    parser.add_argument('--fallback',choices=FALLBACKS)
    args=parser.parse_args()
    if not args.case:
        for key,c in CASES.items():print(key+': '+c['prompt']+'\n')
        print('Routes: '+', '.join(ROUTES))
        print('Fallbacks: usb-app = MicroPython helper; usb-console = C13 console; service-visit = physical service')
        return
    if args.route is None or args.fallback is None:parser.error('--case requires --route and --fallback')
    result=assess(args.case,args.route,args.fallback)
    print(CASES[args.case]['prompt'])
    print('PASS' if result['passed'] else 'TRY AGAIN')
    print('Route: '+result['expected_route']+'. '+result['reason'])
    print('Fallback: '+result['expected_fallback']+'. '+result['recovery'])
    raise SystemExit(0 if result['passed'] else 1)

if __name__=='__main__':main()
