import type { Survey } from "../util/survey-utils/survey";

// The one question-type vocabulary shared by every Autofill Runnable.
export type TQuestionType = "text" | "radio" | "checkbox";

// Checkbox answers are always a list of choices (even when only one is ticked). Text and radio
// answers are always a single value.
export type TAnswerValue<TType extends TQuestionType> = TType extends "checkbox"
  ? string[]
  : string | number;

// Identifies a Store (eg. "T1344"). Keys every Runnable's Store Preset table.
export type TStoreNumber = `T${string}`;

export type TQuestionIdentity = {
  sectionName?: string | RegExp;
  questionLabel: string | RegExp;
};

export type TDirectAnswerPresetOf<TStorePreset, TType extends TQuestionType> = TQuestionIdentity & {
  questionType: TType;
  answerDependency: "directAnswer";
  // Returning `null` leaves the question as a Skipped Question.
  answer: (storePreset: TStorePreset) => TAnswerValue<TType> | null;
};

// Distributed over every question type, so a preset's `answer` must return the value shape that
// matches its own `questionType`.
export type TDirectAnswerPreset<TStorePreset> = {
  [TType in TQuestionType]: TDirectAnswerPresetOf<TStorePreset, TType>;
}[TQuestionType];

export type TNonEmptyNumberArray = [number, ...number[]];

export type TDependentAnswerPresetOf<TType extends TQuestionType> = TQuestionIdentity & {
  questionType: TType;
  answerDependency: "dependentAnswer";
  // Question keys within the same Answer Preset set.
  dependencyKeys: TNonEmptyNumberArray;
  // Receives the values the Autofill computed for `dependencyKeys`, in the same order, as strings
  // (a checkbox dependency's choices are joined with ", "). Returning `null` leaves the question as
  // a Skipped Question.
  answer: (dependencyAnswers: string[]) => TAnswerValue<TType> | null;
};

export type TDependentAnswerPreset = {
  [TType in TQuestionType]: TDependentAnswerPresetOf<TType>;
}[TQuestionType];

export type TAnswerPreset<TStorePreset> =
  TDirectAnswerPreset<TStorePreset> | TDependentAnswerPreset;

// Keys are question keys: numeric, possibly fractional, and only meaningful within their own set.
export type TAnswerPresetSet<TStorePreset> = Record<number, TAnswerPreset<TStorePreset>>;

// The slice of the Survey View the Autofill module writes through. Production passes the real
// `Survey` instance; tests pass an in-memory stand-in.
export type TAutofillSurvey = Pick<Survey, "getStoreNumber" | "answer" | "answerBySection">;

export type TFailedQuestion = TQuestionIdentity & { cause: unknown };

export type TBlockedQuestion = TQuestionIdentity & { failedDependency: TQuestionIdentity };

export type TRunOutcome = {
  filled: TQuestionIdentity[];
  skipped: TQuestionIdentity[];
  blocked: TBlockedQuestion[];
  failed: TFailedQuestion[];
};

export type TConfigurationError =
  | { kind: "storeNumberUnreadable"; message: string; cause: unknown }
  | { kind: "unknownStore"; message: string; storeNumber: string }
  | {
      kind: "missingDependency" | "dependencyNotEarlier";
      message: string;
      questionKey: number;
      dependentQuestion: TQuestionIdentity;
      dependencyKey: number;
    };
