import { isEmpty } from 'lodash';
import { ReturnProcessAnswers, ReturnProcessQuestion } from '../../models';
import { activeReturnProcessQuestions } from './active-return-process-questions.helper';
import { ReturnProcessQuestionSchema } from '../../questions';

/**
 * Union type for parameters accepted by allReturnProcessQuestionsAnswered.
 * Allows passing either the full question set with answers or just the active questions with answers.
 * This flexibility enables performance optimization by avoiding recalculation of active questions
 * when they are already known.
 */
export type AllReturnProcessQuestionsAnsweredParams =
  | {
      questions: ReturnProcessQuestion[];
      answers: ReturnProcessAnswers;
    }
  | {
      activeQuestions: ReturnProcessQuestion[];
      answers: ReturnProcessAnswers;
    };

/**
 * Checks if all required questions in a return process have been answered.
 *
 * This function verifies that all questions that should be answered based on the current
 * state of the return process have valid answers. It can work with either the full set
 * of questions or just the active questions to optimize performance.
 *
 * Features:
 * - Validates all answers using schema validation
 * - Supports both full question sets and pre-filtered active questions
 * - Handles special validation for checklist-type questions
 * - Returns undefined for edge cases where no questions exist
 *
 * @param params - Object containing either the full questions array or active questions array, and the current answers
 * @returns
 * - `true` if all required questions have valid answers
 * - `false` if any required question is missing an answer or has an invalid answer
 * - `undefined` if there are no questions to validate
 *
 * @example
 * ```typescript
 * // Using full question set
 * const allAnswered = allReturnProcessQuestionsAnswered({
 *   questions: returnProcessQuestions,
 *   answers: currentAnswers
 * });
 *
 * // Using active questions
 * const allAnswered = allReturnProcessQuestionsAnswered({
 *   activeQuestions: activeQuestions,
 *   answers: currentAnswers
 * });
 * ```
 */
export function allReturnProcessQuestionsAnswered(
  params: AllReturnProcessQuestionsAnsweredParams,
): boolean | undefined {
  if (isEmpty(params.answers)) {
    return false;
  }

  let activeQuestions: ReturnProcessQuestion[] | undefined;

  if ('questions' in params && Array.isArray(params.questions)) {
    const { questions, answers } = params;
    activeQuestions = activeReturnProcessQuestions(questions, answers);
  } else if (
    'activeQuestions' in params &&
    Array.isArray(params.activeQuestions)
  ) {
    activeQuestions = params.activeQuestions;
  }

  if (!activeQuestions || activeQuestions.length === 0) {
    return undefined;
  }

  return activeQuestions.every((question) => {
    const schema = ReturnProcessQuestionSchema[question.key];
    const parseResult = schema.safeParse(params.answers[question.key]);
    return parseResult.success;
  });
}
