/**
 * Integration Tests: handleGetWorkflowStructure
 *
 * Tests workflow structure retrieval against a real n8n instance.
 * Verifies that only nodes and connections are returned (no parameter data).
 */

import { describe, it, expect, beforeEach, afterEach, afterAll } from 'vitest';
import { createTestContext, TestContext, createTestWorkflowName } from '../utils/test-context';
import { getTestN8nClient } from '../utils/n8n-client';
import { N8nApiClient } from '../../../../src/services/n8n-api-client';
import { SIMPLE_WEBHOOK_WORKFLOW, MULTI_NODE_WORKFLOW } from '../utils/fixtures';
import { cleanupOrphanedWorkflows } from '../utils/cleanup-helpers';
import { createMcpContext } from '../utils/mcp-context';
import { InstanceContext } from '../../../../src/types/instance-context';
import { handleGetWorkflowStructure } from '../../../../src/mcp/handlers-n8n-manager';

describe('Integration: handleGetWorkflowStructure', () => {
  let context: TestContext;
  let client: N8nApiClient;
  let mcpContext: InstanceContext;

  beforeEach(() => {
    context = createTestContext();
    client = getTestN8nClient();
    mcpContext = createMcpContext();
  });

  afterEach(async () => {
    await context.cleanup();
  });

  afterAll(async () => {
    if (!process.env.CI) {
      await cleanupOrphanedWorkflows();
    }
  });

  // ======================================================================
  // Simple Workflow Structure
  // ======================================================================

  describe('Simple Workflow', () => {
    it('should retrieve workflow structure with nodes and connections', async () => {
      // Create a simple workflow
      const workflow = {
        ...SIMPLE_WEBHOOK_WORKFLOW,
        name: createTestWorkflowName('Get Structure - Simple'),
        tags: ['mcp-integration-test']
      };

      const created = await client.createWorkflow(workflow);
      expect(created).toBeDefined();
      expect(created.id).toBeTruthy();

      if (!created.id) throw new Error('Workflow ID is missing');
      context.trackWorkflow(created.id);

      // Retrieve workflow structure
      const response = await handleGetWorkflowStructure({ id: created.id }, mcpContext);
      expect(response.success).toBe(true);
      const structure = response.data as any;

      // Verify structure contains basic info
      expect(structure).toBeDefined();
      expect(structure.id).toBe(created.id);
      expect(structure.name).toBe(workflow.name);

      // Verify nodes are present
      expect(structure.nodes).toBeDefined();
      expect(structure.nodes).toHaveLength(workflow.nodes!.length);

      // Verify connections are present
      expect(structure.connections).toBeDefined();

      // Verify node structure (names and types should be present)
      const node = structure.nodes[0];
      expect(node.id).toBeDefined();
      expect(node.name).toBeDefined();
      expect(node.type).toBeDefined();
      expect(node.position).toBeDefined();
    });
  });

  // ======================================================================
  // Complex Workflow Structure
  // ======================================================================

  describe('Complex Workflow', () => {
    it('should retrieve complex workflow structure without exposing sensitive parameter data', async () => {
      // Create a complex workflow with multiple nodes
      const workflow = {
        ...MULTI_NODE_WORKFLOW,
        name: createTestWorkflowName('Get Structure - Complex'),
        tags: ['mcp-integration-test']
      };

      const created = await client.createWorkflow(workflow);
      expect(created).toBeDefined();
      expect(created.id).toBeTruthy();

      if (!created.id) throw new Error('Workflow ID is missing');
      context.trackWorkflow(created.id);

      // Retrieve workflow structure
      const response = await handleGetWorkflowStructure({ id: created.id }, mcpContext);
      expect(response.success).toBe(true);
      const structure = response.data as any;

      // Verify structure contains all nodes
      expect(structure.nodes).toBeDefined();
      expect(structure.nodes).toHaveLength(workflow.nodes!.length);

      // Verify all connections are present
      expect(structure.connections).toBeDefined();
      expect(Object.keys(structure.connections).length).toBeGreaterThan(0);

      // Verify each node has basic structure
      structure.nodes.forEach((node: any) => {
        expect(node.id).toBeDefined();
        expect(node.name).toBeDefined();
        expect(node.type).toBeDefined();
        expect(node.position).toBeDefined();
        // typeVersion may be undefined depending on API behavior
        if (node.typeVersion !== undefined) {
          expect(typeof node.typeVersion).toBe('number');
        }
      });

      // Note: The actual n8n API's getWorkflowStructure endpoint behavior
      // may vary. Some implementations return minimal data, others return
      // full workflow data. This test documents the actual behavior.
      //
      // If parameters are included, it's acceptable (not all APIs have
      // a dedicated "structure-only" endpoint). The test verifies that
      // the essential structural information is present.
    });
  });
});
