/**
 * ICD Test Runner — CRUD: Memory
 * Memory listing, count shape, error paths, admin endpoints.
 */
(function () {
  'use strict';
  var T = window.ICD;
  if (!T) return;
  if (!T.crud) T.crud = {};

  T.crud.memory = async function (testTag) {

    // ── Memory CRUD ──
    // No REST POST for memories (created by AI tools / background extraction).
    // Test error paths, count shape, and admin endpoints.
    var fakeMemId = '00000000-0000-0000-0000-000000000000';

    await T.test('crud', 'memory', 'GET /memories (list, envelope shape)', async function () {
      var d = await T.apiGet('/memories');
      T.assertHasKey(d, 'data', '/memories');
      T.assert(Array.isArray(d.data), 'data should be array');
      // If memories exist, validate shape
      if (d.data.length > 0) {
        T.assertShape(d.data[0], T.S.memory, 'memory[0]');
      }
    });

    await T.test('crud', 'memory', 'GET /memories?status=pending_review', async function () {
      var d = await T.apiGet('/memories?status=pending_review');
      T.assertHasKey(d, 'data', '/memories?status=pending_review');
      T.assert(Array.isArray(d.data), 'data should be array');
    });

    await T.test('crud', 'memory', 'GET /memories/count (shape)', async function () {
      var d = await T.apiGet('/memories/count');
      T.assertHasKey(d, 'active', '/memories/count');
      T.assertHasKey(d, 'pending', '/memories/count');
      T.assert(typeof d.active === 'number', 'active should be number');
      T.assert(typeof d.pending === 'number', 'pending should be number');
      T.assert(d.active >= 0, 'active should be >= 0');
      T.assert(d.pending >= 0, 'pending should be >= 0');
    });

    await T.test('crud', 'memory', 'PUT /memories/:id (not found → 404)', async function () {
      var token = await T.getAuthToken();
      var d = await T.authFetch(token, 'PUT', '/memories/' + fakeMemId, { value: 'x' });
      T.assertStatus(d, 404, 'update non-existent memory');
    });

    await T.test('crud', 'memory', 'DELETE /memories/:id (not found → 404)', async function () {
      var token = await T.getAuthToken();
      var d = await T.authFetch(token, 'DELETE', '/memories/' + fakeMemId);
      T.assertStatus(d, 404, 'delete non-existent memory');
    });

    await T.test('crud', 'memory', 'POST /memories/:id/approve (not found → 404)', async function () {
      var token = await T.getAuthToken();
      var d = await T.authFetch(token, 'POST', '/memories/' + fakeMemId + '/approve');
      T.assertStatus(d, 404, 'approve non-existent memory');
    });

    await T.test('crud', 'memory', 'POST /memories/:id/reject (not found → 404)', async function () {
      var token = await T.getAuthToken();
      var d = await T.authFetch(token, 'POST', '/memories/' + fakeMemId + '/reject');
      T.assertStatus(d, 404, 'reject non-existent memory');
    });

    if (T.user.role === 'admin') {
      await T.test('crud', 'memory', 'GET /admin/memories/pending (admin)', async function () {
        var d = await T.apiGet('/admin/memories/pending');
        T.assertHasKey(d, 'data', '/admin/memories/pending');
        T.assert(Array.isArray(d.data), 'data should be array');
        if (d.data.length > 0) {
          T.assertShape(d.data[0], T.S.memory, 'pending[0]');
        }
      });

      await T.test('crud', 'memory', 'POST /admin/memories/bulk-approve (empty ids)', async function () {
        var d = await T.apiPost('/admin/memories/bulk-approve', { ids: [] });
        T.assertHasKey(d, 'approved', 'bulk-approve');
        T.assert(d.approved === 0, 'empty ids should approve 0');
      });

      await T.test('crud', 'memory', 'POST /admin/memories/bulk-approve (bad body → 400)', async function () {
        var token = await T.getAuthToken();
        var d = await T.authFetch(token, 'POST', '/admin/memories/bulk-approve', 'not-json');
        T.assertStatus(d, 400, 'bad body');
      });
    }

  };
})();
