/**
 * SDK Test Runner — Domain: admin
 *
 * Tests admin-scoped endpoints. Requires admin role.
 * KNOWN ISSUES: some admin list endpoints use non-standard envelope
 * keys (§E in ICD drift audit).
 */
(function () {
  'use strict';
  var T = window.SDKR;
  if (!T) return;

  T.registerDomain('admin', async function () {

    // Guard: skip if not admin
    if (!window.sw || !sw.isAdmin) {
      await T.test('admin', 'guard', 'skip — not admin', async function () {
        T.assert(true, 'skipped (user is not admin)');
      });
      return;
    }

    // ── Users ──

    await T.test('admin', 'users', 'sw.api.admin.users.list()', {
      sdk:      function () { return sw.api.admin.users.list(); },
      raw:      { method: 'GET', path: '/admin/users' },
      validate: function (r) {
        // KNOWN: backend returns {users: [...]} not {data: [...]}
        var arr = r;
        if (r && r.users) arr = r.users;
        if (r && r.data) arr = r.data;
        T.assert(Array.isArray(arr), 'expected array (maybe wrapped in non-standard key)');
      }
    });

    // ── Configs (provider configs) ──

    await T.test('admin', 'configs', 'sw.api.admin.configs.list()', {
      sdk:      function () { return sw.api.admin.configs.list(); },
      raw:      { method: 'GET', path: '/admin/configs' },
      validate: function (r) {
        // KNOWN: backend returns {configs: [...]} not {data: [...]}
        var arr = r;
        if (r && r.configs) arr = r.configs;
        if (r && r.data) arr = r.data;
        T.assert(Array.isArray(arr), 'expected array');
      }
    });

    // ── Models ──

    await T.test('admin', 'models', 'sw.api.admin.models.list()', {
      sdk:      function () { return sw.api.admin.models.list(); },
      raw:      { method: 'GET', path: '/admin/models' },
      validate: function (r) {
        // KNOWN: backend returns {models: [...]} not {data: [...]}
        var arr = r;
        if (r && r.models) arr = r.models;
        if (r && r.data) arr = r.data;
        T.assert(Array.isArray(arr), 'expected array');
      }
    });

    // ── Settings ──

    await T.test('admin', 'settings', 'sw.api.admin.settings.get()', {
      sdk:      function () { return sw.api.admin.settings.get(); },
      raw:      { method: 'GET', path: '/admin/settings' },
      validate: function (r) { T.assert(typeof r === 'object', 'expected object'); }
    });

    // ── Packages ──

    await T.test('admin', 'packages', 'sw.api.admin.packages.list()', {
      sdk:      function () { return sw.api.admin.packages.list(); },
      raw:      { method: 'GET', path: '/admin/packages' },
      validate: function (r) { T.assert(Array.isArray(r) || (r && Array.isArray(r.data)), 'expected array'); }
    });

    // ── Surfaces ──

    await T.test('admin', 'surfaces', 'sw.api.admin.surfaces.list()', {
      sdk:      function () { return sw.api.admin.surfaces.list(); },
      raw:      { method: 'GET', path: '/admin/surfaces' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    // ── Extensions ──

    await T.test('admin', 'extensions', 'sw.api.admin.extensions.list()', {
      sdk:      function () { return sw.api.admin.extensions.list(); },
      raw:      { method: 'GET', path: '/admin/extensions' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    // ── Tasks ──

    await T.test('admin', 'tasks', 'sw.api.admin.tasks.list()', {
      sdk:      function () { return sw.api.admin.tasks.list(); },
      raw:      { method: 'GET', path: '/admin/tasks' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    // ── Routing ──

    await T.test('admin', 'routing', 'sw.api.admin.routing.policies()', {
      sdk:      function () { return sw.api.admin.routing.policies(); },
      raw:      { method: 'GET', path: '/admin/routing/policies' },
      validate: function (r) { T.assert(Array.isArray(r) || (r && Array.isArray(r.data)), 'expected array'); }
    });

    // ── Provider health ──

    await T.test('admin', 'providers', 'sw.api.admin.providers.health()', {
      sdk:      function () { return sw.api.admin.providers.health(); },
      raw:      { method: 'GET', path: '/admin/providers/health' },
      validate: function (r) { T.assert(typeof r === 'object' || Array.isArray(r), 'expected object or array'); }
    });

    // ── Storage ──

    await T.test('admin', 'storage', 'sw.api.admin.storage.status()', {
      sdk:      function () { return sw.api.admin.storage.status(); },
      raw:      { method: 'GET', path: '/admin/storage/status' },
      validate: function (r) { T.assert(typeof r === 'object', 'expected object'); }
    });

    // ── Vault ──

    await T.test('admin', 'vault', 'sw.api.admin.vault.status()', {
      sdk:      function () { return sw.api.admin.vault.status(); },
      raw:      { method: 'GET', path: '/admin/vault/status' },
      validate: function (r) { T.assert(typeof r === 'object', 'expected object'); }
    });

    // ── Channels (archived) ──

    await T.test('admin', 'channels', 'sw.api.admin.channels.archived()', {
      sdk:      function () { return sw.api.admin.channels.archived(); },
      raw:      { method: 'GET', path: '/admin/channels/archived' },
      validate: function (r) { T.assert(Array.isArray(r) || (r && Array.isArray(r.data)), 'expected array'); }
    });

    // ── Projects ──

    await T.test('admin', 'projects', 'sw.api.admin.projects.list()', {
      sdk:      function () { return sw.api.admin.projects.list(); },
      raw:      { method: 'GET', path: '/admin/projects' },
      validate: function (r) { T.assert(Array.isArray(r) || (r && Array.isArray(r.data)), 'expected array'); }
    });
  });
})();
