import { describe, test } from 'node:test';
import assert from 'node:assert/strict';
import fc from 'fast-check';
import { Arbiter } from '../../src/core/Arbiter.js';

describe.skip('Fast-check: injectable witness invariants', () => {
  test('injectable witness present/absent determinism', () => {
    fc.assert(
      fc.property(
        fc.integer({ min: 1, max: 5 }),
        fc.boolean(),
        (userCount, hasProof) => {
          const arbiter = new Arbiter();
          arbiter.setRelationConfig('mfa', {
            type: 'source',
            relation: 'mfa',
            injectable: true,
            provides: 'Proof'
          });
          arbiter.setRelationConfig('secure_action', {
            type: 'direct',
            relation: 'mfa'
          });

          for (let u = 0; u < userCount; u++) {
            arbiter.addNode(`user:${u}`, 'user');
          }
          arbiter.addNode('resource:0', 'resource');

          if (hasProof) {
            arbiter.addRelation('user:0', 'mfa', 'resource:0', 1.0);
          }

          const result = arbiter.check('user:0', 'secure_action', 'resource:0');

          assert.strictEqual(result.possibility > 0, hasProof);
          if (!hasProof) {
            assert.ok(result.remediation?.options?.length > 0);
          }
        }
      ),
      { numRuns: 40 }
    );
  });

  test('direct injectable witness remediation when missing', () => {
    fc.assert(
      fc.property(
        fc.boolean(),
        fc.boolean(),
        (hasMfa, hasWebauthn) => {
          const arbiter = new Arbiter();
          arbiter.setRelationConfig('mfa', {
            type: 'direct', relation: 'mfa', injectable: true, provides: 'Proof'
          });
          arbiter.setRelationConfig('webauthn', {
            type: 'direct', relation: 'webauthn', injectable: true, provides: 'Proof'
          });

          arbiter.addNode('user:0', 'user');
          arbiter.addNode('resource:0', 'resource');

          if (hasMfa) arbiter.addRelation('user:0', 'mfa', 'resource:0', 1.0);
          if (hasWebauthn) arbiter.addRelation('user:0', 'webauthn', 'resource:0', 1.0);

          const mfaResult = arbiter.check('user:0', 'mfa', 'resource:0');
          const webResult = arbiter.check('user:0', 'webauthn', 'resource:0');

          assert.strictEqual(mfaResult.possibility > 0, hasMfa);
          assert.strictEqual(webResult.possibility > 0, hasWebauthn);

          if (!hasMfa) {
            assert.ok(Array.isArray(mfaResult.remediation?.options), 'mfa missing should give remediation');
            assert.strictEqual(mfaResult.remediation.options[0].relation, 'mfa');
          }
          if (!hasWebauthn) {
            assert.ok(Array.isArray(webResult.remediation?.options), 'webauthn missing should give remediation');
            assert.strictEqual(webResult.remediation.options[0].relation, 'webauthn');
          }
        }
      ),
      { numRuns: 40 }
    );
  });
});
