import { parse } from "node-html-parser";
import { type z } from "zod";
import type { TI18nString } from "../i18n";
import { RESERVED_FIELD_NAMES } from "../reserved-field-names";
import { isLegacyIdCharset, isSafeIdentifier } from "../safe-identifier";
import { FORBIDDEN_IDS, RESERVED_DECLARED_FIELD_NAMES } from "./link-survey-params";
import type { TConditionGroup, TSingleCondition } from "./logic";
import type {
  TActionJumpToQuestion,
  TSurveyLanguage,
  TSurveyLogicAction,
  TSurveyQuestion,
  TSurveyQuestionId,
} from "./types";

/**
 * Checks if a string contains valid HTML markup
 * @param str - The input string to test
 * @returns true if the string contains valid HTML elements, false otherwise
 */
export const isValidHTML = (str: string): boolean => {
  if (!str) return false;

  try {
    const root = parse(str);
    // Check if there are any element nodes (not just text nodes)
    // nodeType 1 = ELEMENT_NODE
    return root.childNodes.some((node) => Number(node.nodeType) === 1);
  } catch {
    return false;
  }
};

/**
 * Extracts text content from an HTML string
 * Works in both browser and Node.js using node-html-parser
 * @param str - The input string (can be HTML or plain text)
 * @returns The extracted text content without HTML tags
 */
export const getTextContent = (str: string): string => {
  if (!str || str.trim() === "") return "";

  if (isValidHTML(str)) {
    try {
      const root = parse(str);
      const textContent = root.textContent;
      return textContent.trim();
    } catch {
      // If parsing fails, treat as plain text
      return str.trim();
    }
  }

  return str.trim();
};

// Defined in `link-survey-params` so the link page can read them without this module's HTML parser.
export {
  FORBIDDEN_IDS,
  LINK_SURVEY_SYSTEM_PARAMS,
  LINK_SURVEY_SYSTEM_PARAM_KEYS,
  RESERVED_DECLARED_FIELD_NAMES,
} from "./link-survey-params";

const FIELD_TO_LABEL_MAP: Record<string, string> = {
  headline: "question",
  subheader: "description",
  buttonLabel: "next button label",
  backButtonLabel: "back button label",
  placeholder: "placeholder",
  upperLabel: "upper label",
  lowerLabel: "lower label",
  "consent.label": "checkbox label",
  dismissButtonLabel: "dismiss button label",
  html: "description",
  cardHeadline: "note",
  welcomeCardHtml: "welcome message",
  endingCardButtonLabel: "button label",
  imageAltText: "image alt text",
};

const extractLanguageCodes = (surveyLanguages?: TSurveyLanguage[]): string[] => {
  if (!surveyLanguages) return [];
  return surveyLanguages.map((surveyLanguage) =>
    surveyLanguage.default ? "default" : surveyLanguage.language.code
  );
};

const validateLabelForAllLanguages = (label: TI18nString, surveyLanguages: TSurveyLanguage[]): string[] => {
  const enabledLanguages = surveyLanguages.filter((lang) => lang.enabled);
  const languageCodes = extractLanguageCodes(enabledLanguages);

  const languages = !languageCodes.length ? ["default"] : languageCodes;
  const invalidLanguageCodes = languages.filter((language) => {
    // Check if label exists and is not undefined
    if (!label[language]) return true;

    // Use getTextContent to extract text from HTML or plain text
    // This ensures empty HTML like <p><br></p> is properly detected as empty
    const textContent = getTextContent(label[language]);
    return textContent.length === 0;
  });

  return invalidLanguageCodes;
};

export const validateQuestionLabels = (
  field: string,
  fieldLabel: TI18nString,
  languages: TSurveyLanguage[],
  questionIndex: number,
  skipArticle = false
): z.core.$ZodRawIssue | null => {
  const invalidLanguageCodes = validateLabelForAllLanguages(fieldLabel, languages);
  const isDefaultMissing = invalidLanguageCodes.includes("default");

  const messagePrefix = skipArticle ? "" : "The ";
  const messageField = FIELD_TO_LABEL_MAP[field] ? FIELD_TO_LABEL_MAP[field] : field;
  const messageSuffix = isDefaultMissing ? " is missing" : " is missing for the following languages: ";

  const message = isDefaultMissing
    ? `${messagePrefix}${messageField} in question ${String(questionIndex + 1)}${messageSuffix}`
    : `${messagePrefix}${messageField} in question ${String(questionIndex + 1)}${messageSuffix} -fLang- ${invalidLanguageCodes.join()}`;

  if (isDefaultMissing) {
    return {
      code: "custom",
      input: fieldLabel,
      message,
      path: ["questions", questionIndex, field],
    };
  }

  // fieldLabel should contain all the keys present in languages
  // even if one of the keys is an empty string, its okay but it shouldn't be undefined

  for (const language of languages) {
    if (
      !language.default &&
      // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- could be undefined
      fieldLabel[language.language.code] === undefined
    ) {
      return {
        code: "custom",
        input: fieldLabel,
        message: `The ${field} in question ${String(questionIndex + 1)} is not present for the following languages: ${language.language.code}`,
        path: ["questions", questionIndex, field],
      };
    }
  }

  if (invalidLanguageCodes.length) {
    return {
      code: "custom",
      input: fieldLabel,
      message,
      path: ["questions", questionIndex, field],
      params: { invalidLanguageCodes },
    };
  }

  return null;
};

export const validateCardFieldsForAllLanguages = (
  field: string,
  fieldLabel: TI18nString,
  languages: TSurveyLanguage[],
  cardType: "welcome" | "end",
  endingCardIndex?: number,
  skipArticle = false
): z.core.$ZodRawIssue | null => {
  const cardTypeLabel =
    cardType === "welcome" ? "Welcome card" : `Ending card ${((endingCardIndex ?? -1) + 1).toString()}`; // Ensure 1-based indexing

  const path = cardType === "welcome" ? ["welcomeCard", field] : ["endings", endingCardIndex ?? -1, field];

  const invalidLanguageCodes = validateLabelForAllLanguages(fieldLabel, languages);
  const isDefaultMissing = invalidLanguageCodes.includes("default");

  const messagePrefix = skipArticle ? "" : "The ";
  const messageField = FIELD_TO_LABEL_MAP[field] ? FIELD_TO_LABEL_MAP[field] : field;
  const messageSuffix = isDefaultMissing ? " is missing" : " is missing for the following languages: ";

  const message = isDefaultMissing
    ? `${messagePrefix}${messageField} on the ${cardTypeLabel}${messageSuffix}`
    : `${messagePrefix}${messageField} on the ${cardTypeLabel}${messageSuffix} -fLang- ${invalidLanguageCodes.join(", ")}`;

  if (isDefaultMissing) {
    return {
      code: "custom",
      input: fieldLabel,
      message,
      path,
    };
  }

  // fieldLabel should contain all the keys present in languages
  // even if one of the keys is an empty string, its okay but it shouldn't be undefined

  for (const language of languages) {
    if (
      !language.default &&
      // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- could be undefined
      fieldLabel[language.language.code] === undefined
    ) {
      return {
        code: "custom",
        input: fieldLabel,
        message: `The ${field} in ${cardTypeLabel} is not present for the following languages: ${language.language.code}`,
        path,
      };
    }
  }

  if (invalidLanguageCodes.length) {
    return {
      code: "custom",
      input: fieldLabel,
      message,
      path,
      params: { invalidLanguageCodes },
    };
  }

  return null;
};

export const findLanguageCodesForDuplicateLabels = (
  labels: TI18nString[],
  surveyLanguages: TSurveyLanguage[]
): string[] => {
  const enabledLanguages = surveyLanguages.filter((lang) => lang.enabled);
  const languageCodes = extractLanguageCodes(enabledLanguages);

  const languagesToCheck = languageCodes.length === 0 ? ["default"] : languageCodes;

  const duplicateLabels = new Set<string>();

  for (const language of languagesToCheck) {
    const labelTexts = labels
      .map((label) => label[language])
      .filter((text): text is string => typeof text === "string" && text.trim().length > 0)
      .map((text) => text.trim());
    const uniqueLabels = new Set(labelTexts);

    if (uniqueLabels.size !== labelTexts.length) {
      duplicateLabels.add(language);
    }
  }

  return Array.from(duplicateLabels);
};

// eslint-disable-next-line typescript/no-deprecated -- validates the legacy question shape the v1 API still accepts
export const findQuestionsWithCyclicLogic = (questions: TSurveyQuestion[]): string[] => {
  const visited: Record<string, boolean> = {};
  const recStack: Record<string, boolean> = {};
  const cyclicQuestions = new Set<string>();

  // eslint-disable-next-line typescript/no-deprecated -- validates the legacy question shape the v1 API still accepts
  const checkForCyclicLogic = (questionId: TSurveyQuestionId): boolean => {
    if (!visited[questionId]) {
      visited[questionId] = true;
      recStack[questionId] = true;

      const question = questions.find((ques) => ques.id === questionId);
      const jumpTargets = (question?.logic ?? [])
        .flatMap((logic) => findJumpToQuestionActions(logic.actions))
        .map((jumpAction) => jumpAction.target);
      if (jumpTargets.some(leadsToCycle)) {
        cyclicQuestions.add(questionId);
        return true;
      }

      // Check fallback logic
      if (question?.logicFallback) {
        if (leadsToCycle(question.logicFallback)) {
          cyclicQuestions.add(questionId);
          return true;
        }
      }

      // Handle default behavior: move to the next question if no jump actions or fallback logic is defined
      const nextQuestionIndex = questions.findIndex((ques) => ques.id === questionId) + 1;
      // eslint-disable-next-line typescript/no-deprecated -- validates the legacy question shape the v1 API still accepts
      const nextQuestion = questions[nextQuestionIndex] as TSurveyQuestion | undefined;
      if (nextQuestion && !visited[nextQuestion.id] && checkForCyclicLogic(nextQuestion.id)) {
        return true;
      }
    }

    recStack[questionId] = false;
    return false;
  };

  /** Whether following `destination` reaches a cycle: unvisited and cyclic, or still on the stack. */
  const leadsToCycle = (destination: string): boolean =>
    (!visited[destination] && checkForCyclicLogic(destination)) || Boolean(recStack[destination]);

  for (const question of questions) {
    checkForCyclicLogic(question.id);
  }

  return Array.from(cyclicQuestions);
};

// Helper function to find all "jumpToQuestion" actions in the logic
// eslint-disable-next-line typescript/no-deprecated -- validates the legacy question shape the v1 API still accepts
const findJumpToQuestionActions = (actions: TSurveyLogicAction[]): TActionJumpToQuestion[] => {
  // eslint-disable-next-line typescript/no-deprecated -- validates the legacy question shape the v1 API still accepts
  return actions.filter((action): action is TActionJumpToQuestion => action.objective === "jumpToQuestion");
};

export enum TValidateIdErrorCode {
  Empty = "empty",
  Duplicate = "duplicate",
  Reserved = "reserved",
  HasSpaces = "has_spaces",
  InvalidChars = "invalid_chars",
  NotSafeIdentifier = "not_safe_identifier",
}

export interface TValidateIdError {
  code: TValidateIdErrorCode;
  field: string;
}

export interface TValidateIdOptions {
  /**
   * Applies the strict shared naming rule (`isSafeIdentifier`) on top of the lenient character
   * check. Use it wherever a *new* declared field name is created — hidden fields, variables,
   * Embedded Data fields — so every new name follows one rule. Element and question ids stay
   * lenient, otherwise renaming a question to `Q1` would start failing.
   */
  requireSafeIdentifier?: boolean;
}

// function to validate hidden field or question id or element id
export const validateId = (
  field: string,
  existingElementIds: string[],
  existingEndingCardIds: string[],
  existingHiddenFieldIds: string[],
  existingVariableNames: string[] = [],
  { requireSafeIdentifier = false }: TValidateIdOptions = {}
): TValidateIdError | null => {
  if (field.trim() === "") {
    return { code: TValidateIdErrorCode.Empty, field };
  }

  const combinedIds = [
    ...existingElementIds,
    ...existingHiddenFieldIds,
    ...existingEndingCardIds,
    ...existingVariableNames,
  ];

  if (combinedIds.findIndex((id) => id.toLowerCase() === field.toLowerCase()) !== -1) {
    return { code: TValidateIdErrorCode.Duplicate, field };
  }

  // Reserved names stay case-sensitive on the lenient path so element and question id renames keep
  // behaving exactly as before. New declared field names are matched case-insensitively and against
  // the link-survey system params too: `getHiddenFieldsFromSearchParams` never fills a field named
  // exactly like one of those params, and never fills any field from a case variant of one (`Verify`
  // from `?verify=`), so a new name colliding with one in any casing must not be creatable.
  //
  // `RESERVED_FIELD_NAMES` (the Tier-1 Embedded Data catalog: country, url, browser, ...) joins them
  // on the strict path ONLY. It must never move into `RESERVED_DECLARED_FIELD_NAMES`, which is also
  // the capture-refusal list read by `getHiddenFieldsFromSearchParams` — putting `country` there
  // would stop `?country=DE` from filling the hidden field of a survey that legitimately declares
  // `country` today. Refused at authoring time; whatever a survey already declares keeps working.
  const isReserved = requireSafeIdentifier
    ? RESERVED_DECLARED_FIELD_NAMES.has(field.toLowerCase()) || RESERVED_FIELD_NAMES.has(field.toLowerCase())
    : FORBIDDEN_IDS.includes(field);

  if (isReserved) {
    return { code: TValidateIdErrorCode.Reserved, field };
  }

  if (field.includes(" ")) {
    return { code: TValidateIdErrorCode.HasSpaces, field };
  }

  if (!isLegacyIdCharset(field)) {
    return { code: TValidateIdErrorCode.InvalidChars, field };
  }

  if (requireSafeIdentifier && !isSafeIdentifier(field)) {
    return { code: TValidateIdErrorCode.NotSafeIdentifier, field };
  }

  return null;
};

type TCondition = TSingleCondition | TConditionGroup;

export const isSingleCondition = (condition: TCondition): condition is TSingleCondition => {
  return "leftOperand" in condition && "operator" in condition;
};

export const isConditionGroup = (condition: TCondition): condition is TConditionGroup => {
  return "conditions" in condition;
};
