/**
 * SDK Test Runner — Domain: tasks
 *
 * KNOWN BUGS:
 *   sw.api.tasks.start(id) → POST /tasks/:id/start  (backend: /tasks/:id/run)
 *   sw.api.tasks.stop(id)  → POST /tasks/:id/stop   (backend: /tasks/:id/kill)
 */
(function () {
  'use strict';
  var T = window.SDKR;
  if (!T) return;

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

    // ── List ──

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

    // ── Create ──

    await T.test('tasks', 'crud', 'sw.api.tasks.create()', {
      sdk: function () {
        return sw.api.tasks.create({
          name: tag + '-task', task_type: 'prompt',
          schedule: '@daily', user_prompt: 'SDK test prompt', is_active: false
        });
      },
      raw: { method: 'POST', path: '/tasks', body: {
        name: tag + '-task', task_type: 'prompt',
        schedule: '@daily', user_prompt: 'SDK test prompt', is_active: false
      }},
      validate: function (r) {
        T.assertShape(r, T.S.task, 'task');
        taskId = r.id;
        T.registerCleanup(function () { if (taskId) return T.safeDelete('/tasks/' + taskId); });
      }
    });

    if (!taskId) return;

    // ── Get ──

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

    // ── Update ──

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

    // ── Start ──

    await T.test('tasks', 'lifecycle', 'sw.api.tasks.start(id)', {
      sdk: function () {
        if (sw.api.tasks.start) return sw.api.tasks.start(taskId);
        throw new Error('sw.api.tasks.start not defined');
      },
      raw: { method: 'POST', path: '/tasks/' + taskId + '/run', body: {} },
      validate: function () { /* 200/202 is sufficient */ }
    });

    // ── Stop ── (404 expected when no active run)

    await T.test('tasks', 'lifecycle', 'sw.api.tasks.stop(id)', {
      sdk: function () {
        if (sw.api.tasks.stop) return sw.api.tasks.stop(taskId);
        throw new Error('sw.api.tasks.stop not defined');
      },
      raw: { method: 'POST', path: '/tasks/' + taskId + '/kill', body: {} },
      validate: function () { /* 200/202 is sufficient */ },
      skip: 'no active run to cancel — 404 expected without task execution'
    });

    // ── Runs ──

    await T.test('tasks', 'runs', 'sw.api.tasks.runs(id)', {
      sdk: function () {
        if (sw.api.tasks.runs) return sw.api.tasks.runs(taskId);
        throw new Error('sw.api.tasks.runs not defined');
      },
      raw: { method: 'GET', path: '/tasks/' + taskId + '/runs' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    // ── Delete ──

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