/**
 * Integration Tests: handleGetWorkflowMinimal
 *
 * Tests minimal workflow data retrieval against a real n8n instance.
 * Returns only ID, name, active status, and tags for fast listing operations.
 */

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 } from '../utils/fixtures';
import { cleanupOrphanedWorkflows } from '../utils/cleanup-helpers';
import { createMcpContext } from '../utils/mcp-context';
import { InstanceContext } from '../../../../src/types/instance-context';
import { handleGetWorkflowMinimal } from '../../../../src/mcp/handlers-n8n-manager';

describe('Integration: handleGetWorkflowMinimal', () => {
  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();
    }
  });

  // ======================================================================
  // Inactive Workflow
  // ======================================================================

  describe('Inactive Workflow', () => {
    it('should retrieve minimal data for inactive workflow', async () => {
      // Create workflow (starts inactive by default)
      const workflow = {
        ...SIMPLE_WEBHOOK_WORKFLOW,
        name: createTestWorkflowName('Get Minimal - Inactive'),
        tags: [
          'mcp-integration-test',
          'minimal-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 minimal workflow data
      const response = await handleGetWorkflowMinimal({ id: created.id }, mcpContext);
      expect(response.success).toBe(true);
      const minimal = response.data as any;

      // Verify only minimal fields are present
      expect(minimal).toBeDefined();
      expect(minimal.id).toBe(created.id);
      expect(minimal.name).toBe(workflow.name);
      expect(minimal.active).toBe(false);

      // Verify tags field (may be undefined in API response)
      // Note: n8n API may not return tags in minimal workflow view
      if (minimal.tags) {
        expect(minimal.tags.length).toBeGreaterThanOrEqual(0);
      }

      // Verify nodes and connections are NOT included (minimal response)
      // Note: Some implementations may include these fields. This test
      // documents the actual API behavior.
      if (minimal.nodes !== undefined) {
        // If nodes are included, it's acceptable - just verify structure
        expect(Array.isArray(minimal.nodes)).toBe(true);
      }
    });
  });

  // ======================================================================
  // Active Workflow
  // ======================================================================

  describe('Active Workflow', () => {
    it('should retrieve minimal data showing active status', async () => {
      // Create workflow
      const workflow = {
        ...SIMPLE_WEBHOOK_WORKFLOW,
        name: createTestWorkflowName('Get Minimal - Active'),
        tags: [
          'mcp-integration-test',
          'minimal-test-active'
        ]
      };

      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);

      // Note: n8n API doesn't support workflow activation via API
      // So we can only test inactive workflows in automated tests
      // The active field should still be present and set to false

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

      // Verify minimal fields
      expect(minimal).toBeDefined();
      expect(minimal.id).toBe(created.id);
      expect(minimal.name).toBe(workflow.name);

      // Verify active field exists
      expect(minimal).toHaveProperty('active');

      // New workflows are inactive by default (can't be activated via API)
      expect(minimal.active).toBe(false);

      // This test documents the limitation: we can verify the field exists
      // and correctly shows inactive status, but can't test active workflows
      // without manual intervention in the n8n UI.
    });
  });
});
