/**
 * SDK Test Runner — Domain: workspaces
 *
 * Tests: list, create, get, update, delete, files.
 */
(function () {
  'use strict';
  var T = window.SDKR;
  if (!T) return;

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

    // Get current user for owner_type/owner_id
    var me = await sw.api.profile.get();
    var userId = me.id;

    // ── List ──

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

    // ── GetDefault (v0.37.18) ──

    var defaultWsId = null;

    await T.test('workspaces', 'getDefault', 'sw.api.workspaces.getDefault()', {
      sdk:      function () { return sw.api.workspaces.getDefault(); },
      raw:      { method: 'GET', path: '/workspaces/default' },
      validate: function (r) {
        T.assertShape(r, T.S.workspace, 'workspace');
        T.assert(r.owner_type === 'user', 'expected owner_type=user');
        T.assert(r.owner_id === userId, 'expected owner_id=current user');
        defaultWsId = r.id;
      }
    });

    // ── GetDefault idempotent ──

    await T.test('workspaces', 'getDefault', 'sw.api.workspaces.getDefault() idempotent', {
      sdk:      function () { return sw.api.workspaces.getDefault(); },
      raw:      { method: 'GET', path: '/workspaces/default' },
      validate: function (r) {
        T.assert(r.id === defaultWsId, 'expected same workspace on second call');
      }
    });

    // ── Create ──

    await T.test('workspaces', 'crud', 'sw.api.workspaces.create()', {
      sdk: function () {
        return sw.api.workspaces.create({ name: tag + '-ws', owner_type: 'user', owner_id: userId });
      },
      raw: { method: 'POST', path: '/workspaces', body: { name: tag + '-ws', owner_type: 'user', owner_id: userId } },
      validate: function (r) {
        T.assertShape(r, T.S.workspace, 'workspace');
        wsId = r.id;
        T.registerCleanup(function () { if (wsId) return T.safeDelete('/workspaces/' + wsId); });
      }
    });

    if (!wsId) return;

    // ── Get ──

    await T.test('workspaces', 'crud', 'sw.api.workspaces.get(id)', {
      sdk:      function () { return sw.api.workspaces.get(wsId); },
      raw:      { method: 'GET', path: '/workspaces/' + wsId },
      validate: function (r) {
        T.assertShape(r, T.S.workspace, 'workspace');
        T.assert(r.id === wsId, 'id mismatch');
      }
    });

    // ── Update (KNOWN BUG: SDK uses PUT, backend expects PATCH) ──

    await T.test('workspaces', 'crud', 'sw.api.workspaces.update(id, data)', {
      sdk: function () {
        return sw.api.workspaces.update(wsId, { name: tag + '-ws-updated' });
      },
      raw: { method: 'PATCH', path: '/workspaces/' + wsId,
             body: { name: tag + '-ws-updated' } },
      validate: function (r) {
        T.assertShape(r, T.S.workspace, 'workspace');
      }
    });

    // ── Files list ──

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

    // ── Delete ──

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