import { bench, describe } from 'vitest';
import { ConfigValidator } from '../../src/services/config-validator';
import { EnhancedConfigValidator } from '../../src/services/enhanced-config-validator';
import { ExpressionValidator } from '../../src/services/expression-validator';
import { WorkflowValidator } from '../../src/services/workflow-validator';
import { NodeRepository } from '../../src/database/node-repository';
import { SQLiteStorageService } from '../../src/services/sqlite-storage-service';
import { N8nNodeLoader } from '../../src/loaders/node-loader';

describe('Validation Performance', () => {
  let workflowValidator: WorkflowValidator;
  let repository: NodeRepository;
  let storage: SQLiteStorageService;

  const simpleConfig = {
    url: 'https://api.example.com',
    method: 'GET',
    authentication: 'none'
  };

  const complexConfig = {
    resource: 'message',
    operation: 'send',
    channel: 'C1234567890',
    text: 'Hello from benchmark',
    authentication: {
      type: 'oAuth2',
      credentials: {
        oauthTokenData: {
          access_token: 'xoxb-test-token'
        }
      }
    },
    options: {
      as_user: true,
      link_names: true,
      parse: 'full',
      reply_broadcast: false,
      thread_ts: '',
      unfurl_links: true,
      unfurl_media: true
    }
  };

  const simpleWorkflow = {
    name: 'Simple Workflow',
    nodes: [
      {
        id: '1',
        name: 'Manual Trigger',
        type: 'n8n-nodes-base.manualTrigger',
        typeVersion: 1,
        position: [250, 300] as [number, number],
        parameters: {}
      },
      {
        id: '2',
        name: 'HTTP Request',
        type: 'n8n-nodes-base.httpRequest',
        typeVersion: 4.2,
        position: [450, 300] as [number, number],
        parameters: {
          url: 'https://api.example.com',
          method: 'GET'
        }
      }
    ],
    connections: {
      '1': {
        main: [
          [
            {
              node: '2',
              type: 'main',
              index: 0
            }
          ]
        ]
      }
    }
  };

  const complexWorkflow = {
    name: 'Complex Workflow',
    nodes: Array.from({ length: 20 }, (_, i) => ({
      id: `${i + 1}`,
      name: `Node ${i + 1}`,
      type: i % 3 === 0 ? 'n8n-nodes-base.httpRequest' : 
            i % 3 === 1 ? 'n8n-nodes-base.slack' : 
            'n8n-nodes-base.code',
      typeVersion: 1,
      position: [250 + (i % 5) * 200, 300 + Math.floor(i / 5) * 150] as [number, number],
      parameters: {
        url: '={{ $json.url }}',
        method: 'POST',
        body: '={{ JSON.stringify($json) }}',
        headers: {
          'Content-Type': 'application/json'
        }
      }
    })),
    connections: Object.fromEntries(
      Array.from({ length: 19 }, (_, i) => [
        `${i + 1}`,
        {
          main: [[{ node: `${i + 2}`, type: 'main', index: 0 }]]
        }
      ])
    )
  };

  beforeAll(async () => {
    storage = new SQLiteStorageService(':memory:');
    repository = new NodeRepository(storage);
    const loader = new N8nNodeLoader(repository);
    await loader.loadPackage('n8n-nodes-base');
    
    workflowValidator = new WorkflowValidator(repository);
  });

  afterAll(() => {
    storage.close();
  });

  // Note: ConfigValidator and EnhancedConfigValidator have static methods,
  // so instance-based benchmarks are not applicable

  bench('validateExpression - simple expression', async () => {
    ExpressionValidator.validateExpression('{{ $json.data }}');
  }, {
    iterations: 5000,
    warmupIterations: 500,
    warmupTime: 500,
    time: 3000
  });

  bench('validateExpression - complex expression', async () => {
    ExpressionValidator.validateExpression('{{ $node["HTTP Request"].json.items.map(item => item.id).join(",") }}');
  }, {
    iterations: 2000,
    warmupIterations: 200,
    warmupTime: 500,
    time: 3000
  });

  bench('validateWorkflow - simple workflow', async () => {
    await workflowValidator.validateWorkflow(simpleWorkflow);
  }, {
    iterations: 500,
    warmupIterations: 50,
    warmupTime: 500,
    time: 3000
  });

  bench('validateWorkflow - complex workflow', async () => {
    await workflowValidator.validateWorkflow(complexWorkflow);
  }, {
    iterations: 100,
    warmupIterations: 10,
    warmupTime: 500,
    time: 3000
  });

  bench('validateWorkflow - connections only', async () => {
    await workflowValidator.validateConnections(simpleWorkflow);
  }, {
    iterations: 1000,
    warmupIterations: 100,
    warmupTime: 500,
    time: 3000
  });

  bench('validateWorkflow - expressions only', async () => {
    await workflowValidator.validateExpressions(complexWorkflow);
  }, {
    iterations: 500,
    warmupIterations: 50,
    warmupTime: 500,
    time: 3000
  });
});