/**
 * SDK Test Runner — Domain: personas
 */
(function () {
  'use strict';
  var T = window.SDKR;
  if (!T) return;

  T.registerDomain('personas', async function () {
    var tag = 'sdkr-' + Date.now();
    var personaId = null;

    // Policy: allow_user_personas is toggled by fixtures.js.
    // If fixtures didn't run (or user isn't admin), create will 403.

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

    await T.test('personas', 'crud', 'sw.api.personas.create()', {
      sdk: function () {
        return sw.api.personas.create({
          name: tag + '-persona', system_prompt: 'You are a test bot.',
          scope: 'personal'
        });
      },
      raw: { method: 'POST', path: '/personas', body: {
        name: tag + '-persona', system_prompt: 'You are a test bot.',
        scope: 'personal'
      }},
      validate: function (r) {
        T.assertShape(r, T.S.persona, 'persona');
        personaId = r.id;
        T.registerCleanup(function () { if (personaId) return T.safeDelete('/personas/' + personaId); });
      }
    });

    if (!personaId) return;

    // NOTE: User-scoped personas only have LIST + CREATE routes.
    // GET/PUT/DELETE /personas/:id are admin-only (ICD gap).
    // Use admin endpoints for full CRUD testing.

    await T.test('personas', 'crud', 'sw.api.admin.personas.update(id, data)', {
      sdk:      function () { return sw.api.admin.personas.update(personaId, { name: tag + '-pers-upd' }); },
      raw:      { method: 'PUT', path: '/admin/personas/' + personaId, body: { name: tag + '-pers-upd' } },
      validate: function (r) { T.assert(typeof r === 'object', 'expected object'); }
    });

    // ── KBs ──

    await T.test('personas', 'kbs', 'sw.api.admin.personas.kbs(id)', {
      sdk:      function () { return sw.api.admin.personas.kbs(personaId); },
      raw:      { method: 'GET', path: '/admin/personas/' + personaId + '/knowledge-bases' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    await T.test('personas', 'crud', 'sw.api.admin.personas.del(id)', {
      sdk:      function () { return sw.api.admin.personas.del(personaId); },
      raw:      { method: 'DELETE', path: '/admin/personas/' + personaId },
      validate: function () { personaId = null; }
    });
  });
})();
