import sys, traceback, os
# Ensure module imports from this folder
sys.path.insert(0, os.path.dirname(__file__))

try:
    from gtfsloader import departure, load_static_gtfs
    import v
    # Ensure cache dict exists for the country
    v.cache.setdefault('be', {})
    # Ensure operators storage exists to avoid KeyError in departure()
    v.operators.setdefault('be', {})
    v.operators['be'].setdefault('sncb', {})
    print('Loading static GTFS for be/sncb (may download)...')
    load_static_gtfs('be', 'sncb')
    print('Calling departure(stop_id="8814001", country="be", operator="sncb")')
    out = departure('8814001', 'be', 'sncb')
    print('RESULT LENGTH:', len(out))
    for i, item in enumerate(out[:20]):
        print(i, item)
except Exception as e:
    print('ERROR:', type(e), e)
    traceback.print_exc()
