import { type } from "os";
import type { QuizQuestionDate } from "./date";
import type { QuizQuestionEmoji } from "./emoji";
import type { QuizQuestionFile } from "./file";
import type { QuizQuestionImages } from "./images";
import type { QuizQuestionNumber } from "./number";
import type { QuizQuestionPage } from "./page";
import type { QuizQuestionRating } from "./rating";
import type { QuizQuestionResult } from "./result";
import type { QuizQuestionSelect } from "./select";
import type { QuizQuestionText } from "./text";
import type { QuizQuestionVariant } from "./variant";
import type { QuizQuestionVarImg } from "./varimg";

export interface QuestionBranchingRuleMain {
  next: string;
  or: boolean;
  rules: {
    question: string; //id родителя (пока что)
    answers: string[];
  }[];
}

export type EditedImagesScreens = "desktop" | "tablet" | "mobile" | "small";
export type EditedUrlImagesList = Record<EditedImagesScreens, string>;

export interface QuestionBranchingRule {
  children: string[];
  //список условий
  main: QuestionBranchingRuleMain[];
  parentId: string | null | "root";
  default: string;
}

export interface QuestionHint {
  /** Текст подсказки */
  text: string;
  /** URL видео подсказки */
  video: string;
}

export type QuestionVariant = {
  id: string;
  /** Текст */
  answer: string;
  /** Текст подсказки */
  hints: string;
  /** Дополнительное поле для текста, emoji, ссылки на картинку */
  extendedText: string;
  isOwn?: boolean;
  isMulti?: boolean;
  /** Оригинал изображения (до кропа) */
  originalImageUrl: string;
  /** Локальный URL для предпросмотра */
  localImageUrl?: string;
  points?: number;
  fileId?: string;
};
export interface QuestionVariantWithEditedImages extends QuestionVariant {
  editedUrlImagesList?: EditedUrlImagesList | null;
}

export type QuestionType =
  | "variant"
  | "images"
  | "varimg"
  | "emoji"
  | "text"
  | "select"
  | "date"
  | "number"
  | "file"
  | "page"
  | "rating"
  | "result";

export interface QuizQuestionBase {
  backendId: number;
  /** Stable id, generated on client */
  id: string;
  quizId: number;
  title: string;
  description: string;
  page: number;
  type?: QuestionType | null;
  expanded: boolean;
  openedModalSettings: boolean;
  deleted: boolean;
  required: boolean;
  deleteTimeoutId: number;
  content: {
    id: string;
    hint: QuestionHint;
    rule: QuestionBranchingRule;
    back: string | null;
    originalBack: string | null;
    autofill: boolean;
  };
}

export type AnyTypedQuizQuestion =
  | QuizQuestionVariant
  | QuizQuestionImages
  | QuizQuestionVarImg
  | QuizQuestionEmoji
  | QuizQuestionText
  | QuizQuestionSelect
  | QuizQuestionDate
  | QuizQuestionNumber
  | QuizQuestionFile
  | QuizQuestionPage
  | QuizQuestionRating
  | QuizQuestionResult;

export type RealTypedQuizQuestion = Exclude<AnyTypedQuizQuestion, QuizQuestionResult>;

type FilterQuestionsWithVariants<T> = T extends {
  content: { variants: QuestionVariant[] };
}
  ? T
  : never;

export type QuizQuestionsWithVariants = FilterQuestionsWithVariants<AnyTypedQuizQuestion>;
