import { describe, it, beforeEach } from '@jest/globals';
import { screen } from '@testing-library/react';
import { renderWithProviders } from '../../../../test-utils/test-utils.tsx';
import TriggerNode, {
  TriggerReduce,
  TriggerNodeCanConnect,
  type TriggerNodeData,
  TriggerConnectionSource, TriggerConnectionTarget
} from '../../../../../src/pages/VisProgPage/visualProgrammingUI/nodes/TriggerNode';
import useFlowStore from '../../../../../src/pages/VisProgPage/visualProgrammingUI/VisProgStores';
import type { Node } from '@xyflow/react';
import '@testing-library/jest-dom';
import { TriggerNodeDefaults } from '../../../../../src/pages/VisProgPage/visualProgrammingUI/nodes/TriggerNode.default.ts';
import { BasicBeliefNodeDefaults } from '../../../../../src/pages/VisProgPage/visualProgrammingUI/nodes/BasicBeliefNode.default.ts';
import { defaultPlan } from '../../../../../src/pages/VisProgPage/visualProgrammingUI/components/Plan.default.ts';
import { NormNodeDefaults } from '../../../../../src/pages/VisProgPage/visualProgrammingUI/nodes/NormNode.default.ts';

describe('TriggerNode', () => {

  beforeEach(() => {
    jest.clearAllMocks();
  });

  describe('Rendering', () => {
    it('should render TriggerNode with keywords type', () => {
      const mockNode: Node<TriggerNodeData> = {
        id: 'trigger-1',
        type: 'trigger',
        position: { x: 0, y: 0 },
        data: {
          ...JSON.parse(JSON.stringify(TriggerNodeDefaults)),
        },
      };

      renderWithProviders(
        <TriggerNode
          id={mockNode.id}
          type={mockNode.type as string}
          data={mockNode.data as any}
          selected={false}
          isConnectable={true}
          zIndex={0}
          dragging={false}
          selectable={true}
          deletable={true}
          draggable={true}
          positionAbsoluteX={0}
          positionAbsoluteY={0}
        />
      );

      expect(screen.getByText(/Triggers when the condition is met/i)).toBeInTheDocument();
      expect(screen.getByText(/Belief is currently/i)).toBeInTheDocument();
      expect(screen.getByText(/Plan is currently/i)).toBeInTheDocument();
    });
  });

  describe('TriggerReduce Function', () => {
    it('should reduce a trigger node to its essential data', () => {
      const conditionNode: Node = {
        id: 'belief-1',
        type: 'basic_belief',
        position: { x: 0, y: 0 },
        data: {
          ...JSON.parse(JSON.stringify(BasicBeliefNodeDefaults)),
        },
      };

      const triggerNode: Node = {
        id: 'trigger-1',
        type: 'trigger',
        position: { x: 0, y: 0 },
        data: {
          ...JSON.parse(JSON.stringify(TriggerNodeDefaults)),
          condition: "belief-1",
          plan: defaultPlan
        },
      };

      useFlowStore.setState({
        nodes: [conditionNode, triggerNode],
        edges: [],
      });

      useFlowStore.getState().onConnect({
        source: 'belief-1',
        target: 'trigger-1',
        sourceHandle: null,
        targetHandle: null,
      });

    const result = TriggerReduce(triggerNode, useFlowStore.getState().nodes);

    expect(result).toEqual({
      id: 'trigger-1',
      condition: {
        id: "belief-1",
        keyword: "help",
      },
      plan: {
        name: "Default Plan",
        id: expect.anything(),
        steps: [],
      },});
    });
  });


  describe('TriggerConnects Function', () => {
    it('should handle connection without errors', () => {
      const node1: Node = {
        id: 'trigger-1',
        type: 'trigger',
        position: { x: 0, y: 0 },
        data: {
          ...JSON.parse(JSON.stringify(TriggerNodeDefaults)),
          label: 'Trigger 1',
        },
      };

      const node2: Node = {
        id: 'norm-1',
        type: 'norm',
        position: { x: 100, y: 0 },
        data: {
          ...JSON.parse(JSON.stringify(NormNodeDefaults)),
          label: 'Norm 1',
        },
      };

      expect(() => {
        TriggerConnectionSource(node1, node2.id);
        TriggerConnectionTarget(node1, node2.id);
      }).not.toThrow();
    });

    it('should return true for TriggerNodeCanConnect if connection exists', () => {
      const connection = { source: 'trigger-1', target: 'norm-1' };
      expect(TriggerNodeCanConnect(connection as any)).toBe(true);
    });
  });
});
