/**
 * 工作流示例
 * 展示如何使用工作流引擎
 */

const { Framework } = require('../src');
const { WorkflowPlugin } = require('../src/capabilities/workflow-engine');

async function main() {
  console.log('=== Workflow Example ===\n');

  // 创建框架
  const framework = new Framework({ debug: true });

  // 加载工作流插件
  await framework.loadPlugin(new WorkflowPlugin());

  console.log('\n--- Testing Script Step ---');

  // 直接执行工作流
  const result1 = await framework.executeTool('execute_workflow', {
    workflow: JSON.stringify({
      steps: [
        {
          type: 'script',
          name: 'Calculate',
          script: `
            const a = 10;
            const b = 20;
            context.variables.sum = a + b;
            return a + b;
          `,
        },
        {
          type: 'script',
          name: 'Multiply',
          script: `
            const sum = context.variables.sum;
            context.variables.product = sum * 2;
            return sum * 2;
          `,
        },
      ],
    }),
    input: {},
  });

  console.log('Result:', result1);

  console.log('\n--- Testing Loop Step ---');

  const result2 = await framework.executeTool('execute_workflow', {
    workflow: JSON.stringify({
      steps: [
        {
          type: 'loop',
          name: 'Loop 3 times',
          maxIterations: 3,
          loopVariable: 'i',
          steps: [
            {
              type: 'script',
              name: 'Log iteration',
              script: `
                console.log('Iteration ' + context.variables.i);
                return context.variables.i;
              `,
            },
          ],
        },
      ],
    }),
    input: {},
  });

  console.log('Result:', result2);

  console.log('\n--- Testing Condition Step ---');

  const result3 = await framework.executeTool('execute_workflow', {
    workflow: JSON.stringify({
      steps: [
        {
          type: 'script',
          name: 'Set value',
          outputVariable: 'value',
          script: `
            context.variables.value = 15;
            return 15;
          `,
        },
        {
          type: 'condition',
          name: 'Check value',
          branches: [
            {
              name: 'Greater than 10',
              condition: 'context.variables.value > 10',
              steps: [
                {
                  type: 'script',
                  name: 'Log greater',
                  script: `console.log('Value is greater than 10'); return 'greater';`,
                },
              ],
            },
            {
              name: 'Less than or equal 10',
              condition: 'context.variables.value <= 10',
              steps: [
                {
                  type: 'script',
                  name: 'Log less',
                  script: `console.log('Value is less than or equal 10'); return 'less-or-equal';`,
                },
              ],
            },
          ],
        },
      ],
    }),
    input: {},
  });

  console.log('Result:', result3);

  console.log('\n--- Testing Delay ---');

  const startTime = Date.now();
  const result4 = await framework.executeTool('execute_workflow', {
    workflow: JSON.stringify({
      steps: [
        {
          type: 'delay',
          name: 'Wait 500ms',
          delayMs: 500,
        },
        {
          type: 'script',
          name: 'Check elapsed',
          script: `
            const elapsed = Date.now() - context.variables.startTime;
            return { elapsed: elapsed, expected: 500 };
          `,
        },
      ],
    }),
    input: {},
  });

  console.log('Result:', result4);

  // 清理
  await framework.destroy();
  console.log('\n[Done]');
}

main().catch(console.error);
