import { GLOBAL_PRESETS } from "../global-presets";
import { setDependentAnswerPreset, setDirectAnswerPreset } from "../util/answer-flow";
import { TAnswerFlow, TDirectAnswer } from "../util/types";
import * as beautyUtils from "../util/utils";

// The Blitz survey asks one combined OOS/stocked count per brand instead of splitting by
// EYE/FACE/LIP category, and skips straight from the after-zoning OOS count to the DPCIs
// needing correction (no intermediate "on hand count greater than 0" reveal question). Each
// brand's flow otherwise has an identical shape, so it's built once per `TBlitzBrandConfig`
// rather than four near-duplicate preset objects.
interface TBlitzBrandConfig {
  oosBeforeQuestion: string;
  totalStockedQuestion: string;
  afterOosQuestion: string;
  dpcisNeedCorrectionQuestion: string;
  teamLeadAssistQuestion: string;
  dpcisCorrectedQuestion: string;
  oosBefore: TDirectAnswer["answer"];
  totalStocked: TDirectAnswer["answer"];
}

function buildBlitzBrandFlows(config: TBlitzBrandConfig): Record<number, TAnswerFlow> {
  return {
    10: setDirectAnswerPreset(undefined, config.oosBeforeQuestion, "text", config.oosBefore),
    20: setDirectAnswerPreset(undefined, config.totalStockedQuestion, "text", config.totalStocked),
    30: setDependentAnswerPreset(
      undefined,
      config.afterOosQuestion,
      "text",
      [10],
      beautyUtils.getAfterOOSCount,
    ),
    40: setDependentAnswerPreset(
      undefined,
      config.dpcisNeedCorrectionQuestion,
      "text",
      [30],
      beautyUtils.getOOSWithOnHandsGreaterThanZero,
    ),
    50: setDependentAnswerPreset(
      undefined,
      config.teamLeadAssistQuestion,
      "radio",
      [40],
      beautyUtils.getDidATeamMemberAssistYouWithUpdatingOnHandsBlitz,
    ),
    60: setDependentAnswerPreset(
      undefined,
      config.dpcisCorrectedQuestion,
      "text",
      [40],
      (dependencyAnswers) => dependencyAnswers[0],
    ),
  };
}

export const BLITZ_NYX_ANSWER_FLOWS: Record<number, TAnswerFlow> = buildBlitzBrandFlows({
  oosBeforeQuestion:
    "UPON ARRIVAL: Before moving, zoning, or stocking any product.\nNYX: How many items are visually out of stock",
  totalStockedQuestion: "How many pieces of NYX did you stock",
  afterOosQuestion: "AFTER Zoning and Stocking\nNYX: How many DPCIs are out of stock",
  dpcisNeedCorrectionQuestion: "NYX: What is the total number DPCIs with incorrect on hand counts",
  teamLeadAssistQuestion:
    "Did a Team Lead assist you during your service to correct any NYX Cosmetics incorrect counts",
  dpcisCorrectedQuestion: "NYX: What is the total number DPCIs in which the counts WERE CORRECTED",
  oosBefore: (storeLabel) =>
    Number(GLOBAL_PRESETS[storeLabel].nyx.eye_oos_before()) +
    Number(GLOBAL_PRESETS[storeLabel].nyx.face_oos_before()) +
    Number(GLOBAL_PRESETS[storeLabel].nyx.lip_oos_before()),
  totalStocked: (storeLabel) => GLOBAL_PRESETS[storeLabel].nyx.total_stocked(),
});

export const BLITZ_LOREAL_ANSWER_FLOWS: Record<number, TAnswerFlow> = buildBlitzBrandFlows({
  oosBeforeQuestion:
    "UPON ARRIVAL: Before moving, zoning, or stocking any product.\nL'Oreal Cosmetics: How many items are visually out of stock",
  totalStockedQuestion: "How many pieces of L'Oreal Cosmetics did you stock",
  afterOosQuestion: "AFTER Zoning and Stocking\nL'Oreal Cosmetics: How many DPCIs are out of stock",
  dpcisNeedCorrectionQuestion:
    "L'Oreal: What is the total number DPCIs in which the counts NEED TO BE CORRECTED",
  teamLeadAssistQuestion:
    "Did a Team Lead assist you during your service to correct any L'Oreal Cosmetics incorrect counts",
  dpcisCorrectedQuestion:
    "L'Oreal: What is the total number DPCIs in which the counts WERE CORRECTED",
  oosBefore: (storeLabel) =>
    Number(GLOBAL_PRESETS[storeLabel].loreal.eye_oos_before()) +
    Number(GLOBAL_PRESETS[storeLabel].loreal.face_oos_before()) +
    Number(GLOBAL_PRESETS[storeLabel].loreal.lip_oos_before()),
  totalStocked: (storeLabel) => GLOBAL_PRESETS[storeLabel].loreal.total_stocked(),
});

export const BLITZ_ESSIE_ANSWER_FLOWS: Record<number, TAnswerFlow> = buildBlitzBrandFlows({
  oosBeforeQuestion:
    "UPON ARRIVAL: Before moving, zoning, or stocking any product.\nessie: How many items are visually out of stock",
  totalStockedQuestion: "How many pieces of essie did you stock",
  afterOosQuestion: "AFTER Zoning and Stocking\nessie: How many DPCIs are out of stock",
  dpcisNeedCorrectionQuestion:
    "essie: What is the total number DPCIs with incorrect on hand counts",
  teamLeadAssistQuestion:
    "Did a Team Lead assist you during your service to correct any Essie Cosmetics incorrect counts",
  dpcisCorrectedQuestion:
    "Essie Nail: What is the total number DPCIs in which the counts WERE CORRECTED",
  oosBefore: (storeLabel) => GLOBAL_PRESETS[storeLabel].essie.oos_before(),
  totalStocked: (storeLabel) => GLOBAL_PRESETS[storeLabel].essie.total_stocked(),
});

export const BLITZ_MAYBELLINE_ANSWER_FLOWS: Record<number, TAnswerFlow> = buildBlitzBrandFlows({
  oosBeforeQuestion:
    "UPON ARRIVAL: Before moving, zoning, or stocking any product.\nMaybelline: How many items are visually out of stock",
  totalStockedQuestion: "How many pieces of Maybelline did you stock",
  afterOosQuestion: "AFTER Zoning and Stocking\nMaybelline: How many DPCIs are out of stock",
  dpcisNeedCorrectionQuestion:
    "Maybelline: What is the total number DPCIs with incorrect on hand counts",
  teamLeadAssistQuestion:
    "Did a Team Lead assist you during your service to correct any Maybelline Cosmetics incorrect counts",
  dpcisCorrectedQuestion:
    "Maybelline: What is the total number DPCIs in which the counts WERE CORRECTED",
  oosBefore: (storeLabel) =>
    Number(GLOBAL_PRESETS[storeLabel].maybelline.eye_oos_before()) +
    Number(GLOBAL_PRESETS[storeLabel].maybelline.face_oos_before()) +
    Number(GLOBAL_PRESETS[storeLabel].maybelline.lip_oos_before()),
  totalStocked: (storeLabel) => GLOBAL_PRESETS[storeLabel].maybelline.total_stocked(),
});
