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

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

    // ── List ──

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

    // ── Create ──

    await T.test('workflows', 'crud', 'sw.api.workflows.create()', {
      sdk: function () {
        return sw.api.workflows.create({
          name: tag + '-wf', slug: tag + '-wf',
          entry_mode: 'team_only', scope: 'personal'
        });
      },
      raw: { method: 'POST', path: '/workflows', body: {
        name: tag + '-wf', slug: tag + '-wf',
        entry_mode: 'team_only', scope: 'personal'
      }},
      validate: function (r) {
        T.assertShape(r, T.S.workflow, 'workflow');
        wfId = r.id;
        T.registerCleanup(function () { if (wfId) return T.safeDelete('/workflows/' + wfId); });
      }
    });

    if (!wfId) return;

    // ── Get ──

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

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

    await T.test('workflows', 'crud', 'sw.api.workflows.update(id, data)', {
      sdk: function () {
        return sw.api.workflows.update(wfId, { name: tag + '-wf-updated' });
      },
      raw: { method: 'PATCH', path: '/workflows/' + wfId,
             body: { name: tag + '-wf-updated' } },
      validate: function (r) {
        T.assertShape(r, T.S.workflow, 'workflow');
        T.assert(r.name === tag + '-wf-updated', 'name not updated');
      }
    });

    // ── Stages ──

    var stageId = null;

    // stages.list via SDK (if wired) vs raw
    await T.test('workflows', 'stages', 'GET /workflows/:id/stages (raw — SDK may not have this)', {
      sdk: function () {
        // SDK may or may not expose stages — test raw path
        if (sw.api.workflows.stages) return sw.api.workflows.stages(wfId);
        throw new Error('sw.api.workflows.stages not defined');
      },
      raw: { method: 'GET', path: '/workflows/' + wfId + '/stages' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    // ── Delete ──

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