/**
 * Test chain performance with actual chain paths (not random pairs)
 */

import { test as _test } from 'node:test';
const test = process.env.RUN_PERF_TESTS === '1' ? _test : _test.skip;
import assert from 'node:assert/strict';
import { BigGraphGenerator } from '../helpers/big-graph-generator.js';
import { ChainRule } from '../../src/authorization/rules/ChainRule.js';

test('measures chain QPS with actual chain paths', async () => {
  if (process.env.TEST_DEBUG === '1') console.log('🔗 Testing chain QPS with actual chain paths...');
  
  const generator = new BigGraphGenerator();
  const graphData = generator.generateGraph('enterprise');
  const arbiter = generator.loadIntoArbiter(graphData);
  
  // Initialize reachability checker with TreeCover strategy
  await arbiter.initializeReachabilityChecker({
    treeCoverOptions: { maxTrees: 3 }
  });
  
  const rule = new ChainRule(arbiter);
  
  // Create a chain rule
  const chainRule = {
    type: 'chain',
    steps: [
      { relation: 'member_of', direction: 'out' },
      { relation: 'can_read', direction: 'out' }
    ],
    collectValues: false,
    valueAggregation: 'sum'
  };
  
  // Find ACTUAL chain paths in the graph
  if (process.env.TEST_DEBUG === '1') console.log('   Finding actual chain paths...');
  
  const actualChains = [];
  
  // Look for users with role memberships
  const roleMemberships = graphData.relations.filter(r => 
    r.relation === 'member_of' && r.dst.startsWith('role:')
  );
  
  if (process.env.TEST_DEBUG === '1') console.log(`   Found ${roleMemberships.length} role memberships`);
  
  // For each role membership, find objects that role can read
  for (const membership of roleMemberships) {
    const userId = membership.src;
    const roleId = membership.dst;
    
    // Find objects this role can read
    const roleReadPermissions = graphData.relations.filter(r => 
      r.relation === 'can_read' && r.src === roleId
    );
    
    for (const permission of roleReadPermissions) {
      actualChains.push({
        user: userId,
        role: roleId,
        object: permission.dst,
        path: `${userId} --[member_of]--> ${roleId} --[can_read]--> ${permission.dst}`
      });
    }
  }
  
  if (process.env.TEST_DEBUG === '1') console.log(`   Found ${actualChains.length} actual chain paths`);
  
  if (actualChains.length === 0) {
    if (process.env.TEST_DEBUG === '1') console.log('   No actual chain paths found, skipping test');
    return;
  }
  
  // Show some examples
  if (process.env.TEST_DEBUG === '1') console.log('   Chain path examples:');
  actualChains.slice(0, 3).forEach(chain => {
    if (process.env.TEST_DEBUG === '1') console.log(`     ${chain.path}`);
  });
  
  // Test with actual chain paths
  if (process.env.TEST_DEBUG === '1') console.log('   Testing with actual chain paths...');
  
  // Test 1: Without caching
  if (process.env.TEST_DEBUG === '1') console.log('   Test 1: Without caching...');
  const start1 = Date.now();
  let queryCount1 = 0;
  let positiveResults1 = 0;
  const end1 = start1 + 3000; // 3 seconds
  
  while (Date.now() < end1) {
    for (const chain of actualChains) {
      // Clear cache to simulate no caching
      rule.chainResultCache.clear();
      rule.chainPathCache.clear();
      
      const result = rule.evaluate(
        arbiter.nodeIdByKey.get(chain.user),
        chain.user,
        arbiter.nodeIdByKey.get(chain.object),
        chain.object,
        chainRule,
        new Set(),
        'can_read_via_role',
        {}
      );
      
      if (result.possibility > 0) {
        positiveResults1++;
      }
      queryCount1++;
    }
  }
  
  const duration1 = Date.now() - start1;
  const qps1 = (queryCount1 / duration1) * 1000;
  const positiveRate1 = (positiveResults1 / queryCount1) * 100;
  
  if (process.env.TEST_DEBUG === '1') console.log(`   Without caching: ${qps1.toFixed(2)} QPS (${queryCount1} queries)`);
  if (process.env.TEST_DEBUG === '1') console.log(`   Positive results: ${positiveResults1} (${positiveRate1.toFixed(1)}%)`);
  
  // Test 2: With caching
  if (process.env.TEST_DEBUG === '1') console.log('   Test 2: With caching...');
  rule.chainResultCache.clear();
  rule.chainPathCache.clear();
  
  const start2 = Date.now();
  let queryCount2 = 0;
  let positiveResults2 = 0;
  const end2 = start2 + 3000; // 3 seconds
  
  while (Date.now() < end2) {
    for (const chain of actualChains) {
      const result = rule.evaluate(
        arbiter.nodeIdByKey.get(chain.user),
        chain.user,
        arbiter.nodeIdByKey.get(chain.object),
        chain.object,
        chainRule,
        new Set(),
        'can_read_via_role',
        {}
      );
      
      if (result.possibility > 0) {
        positiveResults2++;
      }
      queryCount2++;
    }
  }
  
  const duration2 = Date.now() - start2;
  const qps2 = (queryCount2 / duration2) * 1000;
  const positiveRate2 = (positiveResults2 / queryCount2) * 100;
  
  if (process.env.TEST_DEBUG === '1') console.log(`   With caching: ${qps2.toFixed(2)} QPS (${queryCount2} queries)`);
  if (process.env.TEST_DEBUG === '1') console.log(`   Positive results: ${positiveResults2} (${positiveRate2.toFixed(1)}%)`);
  if (process.env.TEST_DEBUG === '1') console.log(`   Cache size: ${rule.chainResultCache.size} results, ${rule.chainPathCache.size} paths`);
  
  // Calculate improvement
  const improvement = qps2 / qps1;
  if (process.env.TEST_DEBUG === '1') console.log(`   QPS improvement: ${improvement.toFixed(2)}x faster`);
  
  // Verify improvement
  assert.ok(improvement > 1, `Caching should improve QPS (${improvement.toFixed(2)}x)`);
  assert.ok(positiveRate1 > 0, 'Should have some positive results');
  assert.ok(positiveRate2 > 0, 'Should have some positive results');
  
  if (process.env.TEST_DEBUG === '1') console.log('   ✅ Actual chain path caching improves QPS');
});

test('measures chain QPS with realistic workload', async () => {
  if (process.env.TEST_DEBUG === '1') console.log('📊 Testing chain QPS with realistic workload...');
  
  const generator = new BigGraphGenerator();
  const graphData = generator.generateGraph('enterprise');
  const arbiter = generator.loadIntoArbiter(graphData);
  
  // Initialize reachability checker with TreeCover strategy
  await arbiter.initializeReachabilityChecker({
    treeCoverOptions: { maxTrees: 3 }
  });
  
  const rule = new ChainRule(arbiter);
  
  // Create a chain rule
  const chainRule = {
    type: 'chain',
    steps: [
      { relation: 'member_of', direction: 'out' },
      { relation: 'can_read', direction: 'out' }
    ],
    collectValues: false,
    valueAggregation: 'sum'
  };
  
  // Create a realistic workload: 70% positive, 30% negative
  if (process.env.TEST_DEBUG === '1') console.log('   Creating realistic workload (70% positive, 30% negative)...');
  
  const workload = [];
  
  // 70% positive queries (actual chain paths)
  const actualChains = [];
  const roleMemberships = graphData.relations.filter(r => 
    r.relation === 'member_of' && r.dst.startsWith('role:')
  );
  
  for (const membership of roleMemberships) {
    const userId = membership.src;
    const roleId = membership.dst;
    
    const roleReadPermissions = graphData.relations.filter(r => 
      r.relation === 'can_read' && r.src === roleId
    );
    
    for (const permission of roleReadPermissions) {
      actualChains.push({
        user: userId,
        object: permission.dst,
        expected: 'positive'
      });
    }
  }
  
  // Take 70% of actual chains for positive queries
  const positiveQueries = actualChains.slice(0, Math.floor(actualChains.length * 0.7));
  
  // 30% negative queries (random pairs that shouldn't have chains)
  const negativeQueries = [];
  const users = graphData.users.slice(0, 3);
  const objects = graphData.documents.slice(0, 3);
  
  for (let i = 0; i < 3; i++) {
    negativeQueries.push({
      user: users[i].key,
      object: objects[i].key,
      expected: 'negative'
    });
  }
  
  workload.push(...positiveQueries, ...negativeQueries);
  
  if (process.env.TEST_DEBUG === '1') console.log(`   Workload: ${positiveQueries.length} positive, ${negativeQueries.length} negative queries`);
  
  // Test 1: Without caching
  if (process.env.TEST_DEBUG === '1') console.log('   Test 1: Without caching...');
  const start1 = Date.now();
  let queryCount1 = 0;
  let positiveCount1 = 0;
  const end1 = start1 + 3000; // 3 seconds
  
  while (Date.now() < end1) {
    for (const query of workload) {
      // Clear cache to simulate no caching
      rule.chainResultCache.clear();
      rule.chainPathCache.clear();
      
      const result = rule.evaluate(
        arbiter.nodeIdByKey.get(query.user),
        query.user,
        arbiter.nodeIdByKey.get(query.object),
        query.object,
        chainRule,
        new Set(),
        'can_read_via_role',
        {}
      );
      
      if (result.possibility > 0) {
        positiveCount1++;
      }
      queryCount1++;
    }
  }
  
  const duration1 = Date.now() - start1;
  const qps1 = (queryCount1 / duration1) * 1000;
  const positiveRate1 = (positiveCount1 / queryCount1) * 100;
  
  if (process.env.TEST_DEBUG === '1') console.log(`   Without caching: ${qps1.toFixed(2)} QPS (${queryCount1} queries)`);
  if (process.env.TEST_DEBUG === '1') console.log(`   Positive rate: ${positiveRate1.toFixed(1)}%`);
  
  // Test 2: With caching
  if (process.env.TEST_DEBUG === '1') console.log('   Test 2: With caching...');
  rule.chainResultCache.clear();
  rule.chainPathCache.clear();
  
  const start2 = Date.now();
  let queryCount2 = 0;
  let positiveCount2 = 0;
  const end2 = start2 + 3000; // 3 seconds
  
  while (Date.now() < end2) {
    for (const query of workload) {
      const result = rule.evaluate(
        arbiter.nodeIdByKey.get(query.user),
        query.user,
        arbiter.nodeIdByKey.get(query.object),
        query.object,
        chainRule,
        new Set(),
        'can_read_via_role',
        {}
      );
      
      if (result.possibility > 0) {
        positiveCount2++;
      }
      queryCount2++;
    }
  }
  
  const duration2 = Date.now() - start2;
  const qps2 = (queryCount2 / duration2) * 1000;
  const positiveRate2 = (positiveCount2 / queryCount2) * 100;
  
  if (process.env.TEST_DEBUG === '1') console.log(`   With caching: ${qps2.toFixed(2)} QPS (${queryCount2} queries)`);
  if (process.env.TEST_DEBUG === '1') console.log(`   Positive rate: ${positiveRate2.toFixed(1)}%`);
  if (process.env.TEST_DEBUG === '1') console.log(`   Cache size: ${rule.chainResultCache.size} results, ${rule.chainPathCache.size} paths`);
  
  // Calculate improvement
  const improvement = qps2 / qps1;
  if (process.env.TEST_DEBUG === '1') console.log(`   QPS improvement: ${improvement.toFixed(2)}x faster`);
  
  // Verify improvement
  assert.ok(improvement > 1, `Caching should improve QPS (${improvement.toFixed(2)}x)`);
  assert.ok(positiveRate1 > 0, 'Should have some positive results');
  assert.ok(positiveRate2 > 0, 'Should have some positive results');
  
  if (process.env.TEST_DEBUG === '1') console.log('   ✅ Realistic workload caching improves QPS');
});
