/**
 * SDK Test Runner — Domain: channels
 *
 * Tests: list, create, get, update, delete, messages, participants,
 * models, kbs, files, markRead, generateTitle, path, siblings.
 */
(function () {
  'use strict';
  var T = window.SDKR;
  if (!T) return;

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

    // ── List ──

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

    // ── Create ──

    await T.test('channels', 'crud', 'sw.api.channels.create() returns channel shape', {
      sdk: function () {
        return sw.api.channels.create({ title: tag + '-ch', type: 'direct' });
      },
      raw: { method: 'POST', path: '/channels', body: { title: tag + '-ch-raw', type: 'direct' } },
      validate: function (r) {
        T.assertShape(r, T.S.channel, 'channel');
        channelId = r.id;
        T.registerCleanup(function () { if (channelId) return T.safeDelete('/channels/' + channelId); });
      }
    });

    if (!channelId) return;

    // ── Get ──

    await T.test('channels', 'crud', 'sw.api.channels.get(id) returns full shape', {
      sdk:      function () { return sw.api.channels.get(channelId); },
      raw:      { method: 'GET', path: '/channels/' + channelId },
      validate: function (r) {
        T.assertShape(r, T.S.channelFull, 'channelFull');
        T.assert(r.id === channelId, 'id mismatch');
      }
    });

    // ── Update ──

    await T.test('channels', 'crud', 'sw.api.channels.update(id, data)', {
      sdk:      function () { return sw.api.channels.update(channelId, { title: tag + '-updated' }); },
      raw:      { method: 'PUT', path: '/channels/' + channelId, body: { title: tag + '-updated' } },
      validate: function (r) {
        T.assertShape(r, T.S.channelFull, 'channel');
        T.assert(r.title === tag + '-updated', 'title not updated');
      }
    });

    // ── Messages ──

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

    // ── Send message ──

    var msgId = null;
    await T.test('channels', 'messages', 'sw.api.channels.send(id, data)', {
      sdk: function () {
        return sw.api.channels.send(channelId, { content: 'sdk-test-msg', role: 'user' });
      },
      raw: { method: 'POST', path: '/channels/' + channelId + '/messages',
             body: { content: 'sdk-test-msg', role: 'user' } },
      validate: function (r) {
        T.assertShape(r, T.S.message, 'message');
        msgId = r.id;
      }
    });

    // ── Siblings ──

    if (msgId) {
      await T.test('channels', 'messages', 'sw.api.channels.siblings(id, msgId)', {
        sdk:      function () { return sw.api.channels.siblings(channelId, msgId); },
        raw:      { method: 'GET', path: '/channels/' + channelId + '/messages/' + msgId + '/siblings' },
        validate: function (r) {
          // Siblings returns { siblings: [...], current_index, total }
          var arr = r.siblings || T.unwrapList(r);
          T.assert(Array.isArray(arr), 'expected siblings array');
        }
      });
    }

    // ── Path ──

    await T.test('channels', 'tree', 'sw.api.channels.path(id)', {
      sdk:      function () { return sw.api.channels.path(channelId); },
      raw:      { method: 'GET', path: '/channels/' + channelId + '/path' },
      validate: function (r) { T.assert(Array.isArray(r), 'expected array'); }
    });

    // ── Mark Read ──

    await T.test('channels', 'read', 'sw.api.channels.markRead(id)', {
      sdk:      function () { return sw.api.channels.markRead(channelId); },
      raw:      { method: 'POST', path: '/channels/' + channelId + '/mark-read', body: {} },
      validate: function () { /* 200 OK is sufficient */ }
    });

    // ── KBs ──

    await T.test('channels', 'kbs', 'sw.api.channels.kbs(id) returns array', {
      sdk:      function () { return sw.api.channels.kbs(channelId); },
      raw:      { method: 'GET', path: '/channels/' + channelId + '/knowledge-bases' },
      validate: function (r) { T.assert(Array.isArray(r), 'expected array'); }
    });

    // ── Files ──

    await T.test('channels', 'files', 'sw.api.channels.files(id)', {
      sdk:      function () { return sw.api.channels.files(channelId); },
      raw:      { method: 'GET', path: '/channels/' + channelId + '/files' },
      validate: function (r) {
        // May be array or {files: [], count: N}
        T.assert(typeof r === 'object' || Array.isArray(r), 'expected object or array');
      }
    });

    // ── Files with origin filter (v0.37.18) ──

    await T.test('channels', 'files', 'sw.api.channels.files(id, {origin})', {
      sdk:      function () { return sw.api.channels.files(channelId, { origin: 'tool_output' }); },
      raw:      { method: 'GET', path: '/channels/' + channelId + '/files?origin=tool_output' },
      validate: function (r) {
        T.assert(typeof r === 'object' || Array.isArray(r), 'expected object or array');
      }
    });

    // ── Participants ──

    await T.test('channels', 'participants', 'sw.api.channels.participants(id)', {
      sdk:      function () { return sw.api.channels.participants(channelId); },
      raw:      { method: 'GET', path: '/channels/' + channelId + '/participants' },
      validate: function (r) { T.assert(Array.isArray(r), 'expected array'); }
    });

    // ── Models ──

    await T.test('channels', 'models', 'sw.api.channels.models(id)', {
      sdk:      function () { return sw.api.channels.models(channelId); },
      raw:      { method: 'GET', path: '/channels/' + channelId + '/models' },
      validate: function (r) { T.assert(Array.isArray(r), 'expected array'); }
    });

    // ── Delete ──

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