import styles from "./styles.module.scss";
import { useNavigate } from "react-router-dom";
import { useUpdateStep } from "@/components/ChatsPath/hooks/useUpdateStep";
import routes from "@/routes";
import { IAnswer } from "@/components/ChatsPath/data";
import Answers from "@/components/ChatsPath/components/Answers";
import AnswerDescription, {
  IAnswerDescriptionProps,
} from "@/components/ChatsPath/components/AnswerDescription";
import { useSelector } from "react-redux";
import { selectors } from "@/store";
import Button from "@/components/ChatsPath/ui/Button";
import { useSession } from "@/hooks/session/useSession";
import { ESourceAuthorization } from "@/api/resources/User";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";

function ProneToOverthinking() {
  const { translate } = useTranslations(ELocalesPlacement.Chats);
  const { updateSession } = useSession();
  useUpdateStep(12);
  const navigate = useNavigate();
  const answer = useSelector(selectors.selectAnswers)?.proneToOverthinking;

  const answers: IAnswer[] = [
    {
      id: 1,
      value: "yes",
      name: translate("yes"),
      questionId: "proneToOverthinking",
    },
    {
      id: 2,
      value: "sometimes",
      name: translate("sometimes"),
      questionId: "proneToOverthinking",
    },
    {
      id: 3,
      value: "no",
      name: translate("no"),
      questionId: "proneToOverthinking",
    },
  ];

  const descriptions: Record<string, IAnswerDescriptionProps> = {
    yes: {
      title: translate("/quiz/proneToOverthinking.description_title_1"),
      description: translate("/quiz/proneToOverthinking.description_text_1"),
    },
    sometimes: {
      title: translate("/quiz/proneToOverthinking.description_title_2"),
      description: translate("/quiz/proneToOverthinking.description_text_2"),
    },
    no: {
      title: translate("/quiz/proneToOverthinking.description_title_3"),
      description: translate("/quiz/proneToOverthinking.description_text_3"),
    },
  };

  const handleNext = async () => {
    const _answer = answers.find((item) => item.value === answer);
    if (_answer) {
      await updateSession(
        {
          answers: {
            proneToOverthinking: _answer.name as string,
          },
        },
        ESourceAuthorization["aura.chats"]
      );
    }
    navigate(routes.client.chatsQuizWorriesImpact());
  };

  return (
    <>
      <h2 className={`chats-answers-title`}>Are you prone to overthinking?</h2>
      <Answers answers={answers} activeAnswer={answer} />
      {!!answer && (
        <AnswerDescription
          title={descriptions[answer]?.title}
          description={descriptions[answer]?.description}
        />
      )}
      {!!answer && (
        <Button classNameContainer={styles.button} onClick={handleNext}>
          {translate("continue")}
        </Button>
      )}
    </>
  );
}

export default ProneToOverthinking;
