import * as helpers from './calculate-longest-question-depth.helper';
import {
  ReturnProcessQuestion,
  ReturnProcessQuestionType,
  ReturnProcessAnswers,
  ReturnProcessQuestionKey,
} from '../../models';
import { tolinoQuestions } from '../../questions/tolino';
import { ItemConditionAnswer } from '../../questions';

describe('calculateLongestQuestionDepth', () => {
  // Setup test data
  const mockQuestions: ReturnProcessQuestion[] = [
    {
      key: ReturnProcessQuestionKey.ItemCondition,
      type: ReturnProcessQuestionType.Select,
      description: 'Question 1',
      options: [
        {
          label: 'Option 1',
          value: 'opt1',
          nextQuestion: ReturnProcessQuestionKey.ReturnReason,
        },
      ],
    },
    {
      key: ReturnProcessQuestionKey.DeliveredItem,
      type: ReturnProcessQuestionType.Select,
      description: 'Question 3',
      options: [],
    },
  ];

  const mockAnswers: ReturnProcessAnswers = {
    [ReturnProcessQuestionKey.ItemCondition]: 'opt1',
    [ReturnProcessQuestionKey.ItemDefective]: true,
  };

  beforeEach(() => {
    // Spy on the internal function while preserving its implementation
    jest.spyOn(helpers, 'internalCalculateLongestQuestionDepth');
  });

  afterEach(() => {
    jest.restoreAllMocks();
  });

  it('should calculate the correct depth for a question flow', () => {
    // Arrange
    const expectedDepth = 1; // Based on our mock data structure

    // Act
    const result = helpers.calculateLongestQuestionDepth(
      mockQuestions,
      mockAnswers,
    );

    // Assert
    expect(result).toBe(expectedDepth);
    expect(typeof result).toBe('number');
  });

  it('should call internalCalculateLongestQuestionDepth with correct parameters', () => {
    jest
      .spyOn(helpers, 'internalCalculateLongestQuestionDepth')
      .mockReturnValue({ depth: 1 });

    // Arrange & Act
    helpers.calculateLongestQuestionDepth(mockQuestions, mockAnswers);

    // Assert
    expect(helpers.internalCalculateLongestQuestionDepth).toHaveBeenCalledWith(
      mockQuestions,
      mockAnswers,
      mockQuestions[0].key,
    );
    expect(helpers.internalCalculateLongestQuestionDepth).toHaveBeenCalledTimes(
      1,
    );
  });
});

describe('internalCalculateLongestQuestionDepth', () => {
  const answers: ReturnProcessAnswers = {
    [ReturnProcessQuestionKey.ItemCondition]: ItemConditionAnswer.Damaged,
    // [ReturnProcessQuestionKey.DevicePower]: YesNoAnswer.Yes,
    // [ReturnProcessQuestionKey.PackageComplete]: YesNoAnswer.Yes,
  };

  afterEach(() => {
    jest.restoreAllMocks();
  });

  it('should return depth 0 if no question found', () => {
    const expectedDepth = 0;

    const result = helpers.internalCalculateLongestQuestionDepth(
      [],
      answers,
      ReturnProcessQuestionKey.ItemCondition,
    );

    expect(result).toEqual({ depth: expectedDepth });
  });

  it('should return depth 1 if no question found', () => {
    const answers: ReturnProcessAnswers = {
      [ReturnProcessQuestionKey.ItemCondition]: ItemConditionAnswer.Damaged,
    };

    const expectedDepth = 9;

    const result = helpers.calculateLongestQuestionDepth(
      tolinoQuestions,
      answers,
    );

    expect(result).toBe(expectedDepth);
  });
});
