/**
 * SDK Test Runner — Domain: connections (v0.38.1)
 *
 * Tests extension connection CRUD and scope resolution.
 * Requires admin role (global connection CRUD).
 */
(function () {
  'use strict';
  var T = window.SDKR;
  if (!T) return;

  T.registerDomain('connections', async function () {

    if (!window.sw || !sw.isAdmin) {
      await T.test('connections', 'guard', 'skip — not admin', async function () {
        T.assert(true, 'skipped (user is not admin)');
      });
      return;
    }

    var globalId = null;
    var personalId = null;

    // ── Global CRUD ──────────────────────────

    await T.test('connections', 'admin-create', 'create global connection', async function () {
      var r = await sw.api.admin.connections.create({
        type: 'sdk-test-conn',
        package_id: 'sdk-test-pkg',
        name: 'Global Test',
        config: { base_url: 'https://test.example.com', org: 'test-org' }
      });
      T.assert(r && r.id, 'should return id');
      T.assert(r.type === 'sdk-test-conn', 'type matches');
      globalId = r.id;
    });

    T.cleanup.push(async function () {
      if (globalId) try { await sw.api.admin.connections.del(globalId); } catch (_) {}
    });

    await T.test('connections', 'admin-list', 'list global connections includes created', async function () {
      var r = await sw.api.admin.connections.list();
      T.assert(Array.isArray(r), 'should be array');
      var found = r.find(function (c) { return c.id === globalId; });
      T.assert(found, 'created connection should be in list');
      T.assert(found.type === 'sdk-test-conn', 'type matches');
      T.assert(found.name === 'Global Test', 'name matches');
    });

    await T.test('connections', 'admin-update', 'update global connection', async function () {
      await sw.api.admin.connections.update(globalId, { name: 'Global Test Updated' });
      var list = await sw.api.admin.connections.list();
      var found = list.find(function (c) { return c.id === globalId; });
      T.assert(found && found.name === 'Global Test Updated', 'name should be updated');
    });

    // ── Personal CRUD ────────────────────────

    await T.test('connections', 'personal-create', 'create personal connection', async function () {
      var r = await sw.api.connections.create({
        type: 'sdk-test-conn',
        package_id: 'sdk-test-pkg',
        name: 'Personal Test',
        config: { base_url: 'https://personal.example.com' }
      });
      T.assert(r && r.id, 'should return id');
      personalId = r.id;
    });

    T.cleanup.push(async function () {
      if (personalId) try { await sw.api.connections.del(personalId); } catch (_) {}
    });

    await T.test('connections', 'personal-list', 'list personal connections', async function () {
      var r = await sw.api.connections.list();
      T.assert(Array.isArray(r), 'should be array');
      var found = r.find(function (c) { return c.id === personalId; });
      T.assert(found, 'personal connection in list');
    });

    // ── Scope Resolution ─────────────────────

    await T.test('connections', 'resolve-prefers-personal', 'resolve prefers personal over global', async function () {
      var r = await sw.api.connections.resolve('sdk-test-conn');
      T.assert(r && r.id, 'should resolve');
      // Personal scope should win over global
      T.assert(r.scope === 'personal', 'should resolve personal scope first');
      T.assert(r.id === personalId, 'should be the personal connection');
    });

    await T.test('connections', 'resolve-by-name', 'resolve by name returns specific connection', async function () {
      var r = await sw.api.connections.resolve('sdk-test-conn', 'Global Test Updated');
      T.assert(r && r.id, 'should resolve');
      T.assert(r.id === globalId, 'should match global connection by name');
    });

    // ── Connection Type Discovery (v0.38.4) ──

    await T.test('connections', 'types-list', 'GET /connection-types returns array', async function () {
      var r = await sw.api.connectionTypes.list();
      T.assert(Array.isArray(r), 'should be array');
    });

    await T.test('connections', 'types-include-sdk-test', 'connection types include sdk-test-conn from global conn', async function () {
      // The global connection we created above references package_id sdk-test-pkg
      // but that package may not exist. The endpoint scans active package manifests,
      // so this test verifies the endpoint returns without error. Actual type matching
      // is tested in the composition domain with a real library.
      var r = await sw.api.connectionTypes.list();
      T.assert(Array.isArray(r), 'should be array (may be empty if no packages declare connections)');
    });

    // ── Cleanup ──────────────────────────────

    await T.test('connections', 'personal-delete', 'delete personal connection', async function () {
      await sw.api.connections.del(personalId);
      var list = await sw.api.connections.list();
      var found = list.find(function (c) { return c.id === personalId; });
      T.assert(!found, 'should be deleted');
      personalId = null;
    });

    await T.test('connections', 'admin-delete', 'delete global connection', async function () {
      await sw.api.admin.connections.del(globalId);
      var list = await sw.api.admin.connections.list();
      var found = list.find(function (c) { return c.id === globalId; });
      T.assert(!found, 'should be deleted');
      globalId = null;
    });

  });
})();
