from pathlib import Path

root = Path(__file__).resolve().parents[1]
app = (root / 'public/app.php').read_text()
sync = (root / 'public/assets/backend-sync.js').read_text()
state = (root / 'public/api/state.php').read_text()
search = (root / 'public/api/global-search.php').read_text()

checks = {
    'version 1.28 features retained': 'VERSION 1.30' in app and (root / 'VERSION').read_text().strip() == '1.30.0-server',
    'empty frontend stores': all(x in app for x in ['const employees = [];', 'const projects = [];', 'const materials = [];']),
    'no automatic demo persistence': 'await persistCoreState();' not in sync,
    'section patch save': 'patch: currentServerState(section)' in sync,
    'optimistic version': 'expected_version:serverStateVersion' in sync and "'STATE_CONFLICT'" in state,
    'server merge': 'array_replace($current,$cleanPatch)' in state,
    'revision audit': "app_state_revisions" in state,
    'modal waits for server': 'async function submitFormModal()' in app and 'if(!window.__lastRequestFailed)closeFormModal()' in app,
    'generic modal does not save whole state': "scheduleServerSave();\n}" not in app,
    'production dashboard': 'dashboard: renderProductionDashboard' in app,
    'mobile admin drawer': 'body.sidebar-open:not(.employee-mobile)' in app,
    'accessible dialogs': 'aria-modal="true"' in app and 'aria-live="polite"' in app,
    'ranked search': "['_rank']" in search and "has_more" in search,
    'honest integrations': 'api/ai-assistant.php' in app and 'للقراءة فقط' in app and 'وضع الباركود والعمل دون اتصال غير مفعّل' in app,
}

failed = [name for name, ok in checks.items() if not ok]
if failed:
    raise SystemExit('FAILED: ' + ', '.join(failed))
print('OK: production-only data, conflict-safe partial saving, resilient forms, responsive admin, accessibility, and ranked search verified')
