/**
 * SDK Test Runner — Entry Point
 *
 * Load order:
 *   1. framework.js   — dual-path harness, assertions, raw fetch
 *   2. shapes.js      — ICD response shapes
 *   3. domains/*.js   — per-domain test suites (register on T.domains)
 *   4. ui.js          — render functions
 *   5. (this file)    — boot
 */
(async function () {
  'use strict';

  var mount = document.getElementById('extension-mount');
  if (!mount) return;

  // ─── Boot SDK ──────────────────────────────────────────────

  try {
    var base = window.__BASE__ || '';
    var ver  = window.__VERSION__ || '0';

    if (!window.preact) {
      var { h, render } = await import(base + '/js/sw/vendor/preact.module.js');
      var hooksModule = await import(base + '/js/sw/vendor/hooks.module.js');
      var htmModule = await import(base + '/js/sw/vendor/htm.module.js');
      window.preact = { h, render };
      window.hooks = hooksModule;
      window.html = htmModule.default.bind(h);
    }

    var sdk = await import(base + '/js/sw/sdk/index.js?v=' + ver);
    await sdk.boot();
    console.log('[SDKR] SDK booted — window.sw ready');
  } catch (e) {
    console.warn('[SDKR] SDK boot failed:', e.message);
  }

  // ─── Shared Namespace ──────────────────────────────────────

  window.SDKR = {
    mount:    mount,
    manifest: window.__MANIFEST__ || {},
    base:     window.__BASE__ || '',
    // Populated by framework.js
    results:  [],
    cleanup:  [],
    stats:    {},
    domains:  {},
    S:        {}
  };

  // ─── Script Loader ─────────────────────────────────────────

  var surfaceId = window.SDKR.manifest.id || 'sdk-test-runner';
  var assetBase = '/surfaces/' + surfaceId + '/js/';

  if (window.SDKR.base) {
    assetBase = window.SDKR.base + assetBase;
  }

  var modules = [
    'framework.js',
    'shapes.js',
    'fixtures.js',
    // Domain test suites
    'domains/channels.js',
    'domains/notes.js',
    'domains/projects.js',
    'domains/personas.js',
    'domains/knowledge.js',
    'domains/workflows.js',
    'domains/workspaces.js',
    'domains/tasks.js',
    'domains/misc.js',
    'domains/admin.js',
    'domains/packages.js',
    'domains/connections.js',
    'domains/dependencies.js',
    'domains/composition.js',
    'domains/git-board.js',
    // UI (must come last)
    'ui.js'
  ];

  var loaded = 0;

  function loadNext() {
    if (loaded >= modules.length) {
      boot();
      return;
    }
    var script = document.createElement('script');
    script.src = assetBase + modules[loaded] + '?v=' + (window.__VERSION__ || '0') + '.' + Date.now();
    script.onload = function () { loaded++; loadNext(); };
    script.onerror = function () {
      console.error('[SDKR] Failed to load: ' + modules[loaded]);
      loaded++; loadNext();
    };
    document.body.appendChild(script);
  }

  function boot() {
    if (typeof window.SDKR.renderShell === 'function') {
      window.SDKR.renderShell();
    } else {
      mount.innerHTML = '<p style="color:var(--danger);padding:24px;">SDK Test Runner failed to load modules. Check console.</p>';
    }
  }

  loadNext();
})();
